Chrome扩展程序-使用JQuery触发content_scripts上的事件 [英] Chrome Extension - Trigger events on content_scripts using JQuery

查看:479
本文介绍了Chrome扩展程序-使用JQuery触发content_scripts上的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Chrome扩展程序,它会自动填写一些注册表格.为了启动一些Ajax调用,需要在"更改"事件上触发一些选择字段.

首先,我使用JQuery attr或val来更改选择字段的值,然后使用 .trigger 来调用"change"事件,但这最后一个不起作用./p>

示例:

我想选择包含单词"London"的选项并调用 更改元素,以便启动本机的某些操作 在更改"事件中具有某些侦听器的代码

jQuery("#SelectElement option:contains('London')").attr("selected", "selected"); 
jQuery("#SelectElement").trigger("change"); <--- not works

我也尝试过:

jQuery("#SelectElement option:containt('London')").attr("selected", "selected").change();

但是,如果我在控制台上尝试使用此代码,它将起作用.

建议?

解决方案

我遇到了同样的问题,据我所知,这是由于所谓的框架事件侦听器引起的.您无法通过jquery从代码中触发!但是解决方案是通过这种方式触发事件:

$(selector)[0].dispatchEvent(new Event("eventName"))

I wrote a Chrome Extension that automatically fills some registration forms. There are some select fields that need to be triggered on "change" event in order to start some Ajax calls.

First I use JQuery attr or val to change the value of the select field, and than I use .trigger to invoke the "change" event, but this last one doesn't work.

Example:

I want to select the option that contains the word "London" and invoke the change element in order to start some operations of the native code that have some listeners on "change" event

jQuery("#SelectElement option:contains('London')").attr("selected", "selected"); 
jQuery("#SelectElement").trigger("change"); <--- not works

I tried also:

jQuery("#SelectElement option:containt('London')").attr("selected", "selected").change();

But if I try this code on console, it works.

Suggestions?

解决方案

I had the same problem and as far as I know it's because of something called framework event listeners. that you cannot trigger from your code by jquery! but the solution is trigger the event this way:

$(selector)[0].dispatchEvent(new Event("eventName"))

这篇关于Chrome扩展程序-使用JQuery触发content_scripts上的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆