Stackoverflow社区,你好。我在此寻求一些帮助,帮助您了解如何使用Google Chrome浏览器扩展程序的消息API将"this"
与the id of "this"
一起发送。Chrome扩展|使用Chrome API发送“this”和“this”id值,如消息
相关HTML代码中使用(popup.html)
<p class="field switch bpos">
<label class="cb-enable" id="id1"><span>yes</span></label>
<label class="cb-disable selected"><span>no</span></label>
<input type="checkbox" id="checkbox" class="checkbox">
</p>
Listener.js(外部脚本)(在popup.html的放置脚本标记内)
$(".cb-enable").click(function() {
chrome.tabs.executeScript(null, {file:"script.js"});
});
的script.js(从{file:“script.js”})运行
function check(){
if ($(this).is('#id1')) {function1(); }
else if ($(this).is('#id2')) {function2(); }
else{alert("id not found");}
}
check();
对于Chrome扩展API相对较新,使用file和s使用消息传递API将信息从一个脚本结束到另一个脚本让我感到相当困惑,并希望你们中的一个能够帮助我确定"this"
与the ID value of this
是如何从一个文件发送到另一个文件的。
如果任何人都可以帮助即使在这个具体要求中的最小的,你的帮助将不可思议地赞赏。
你做出非常正确的观点,我知道它可能看起来与建筑有点奇怪。请原谅我对你的问题的了解,但是一旦点击了具有.cb-enable特定类的元素,它是否会发送要在if语句中使用的“this”id?也感谢你相当快速的回答。 – Fearcustard
是的,我附上的代码 - msg var,在您的script.js中收到的将包含您在popup.html中使用class .cb-enable单击的元素的id。但只有ID,不完整的实体。 – MobDev
你可以在你的script.js中做这样的事情:'if(msg ==“id1”){...}' – MobDev