【问题标题】:Reference to jQuery.js in background_page causes jQuery not working在 background_page 中引用 jQuery.js 会导致 jQuery 无法正常工作
【发布时间】:2012-07-08 13:54:06
【问题描述】:

我在 content_scripts 下的 manifest.json 中定义了 jQuery.js

"background_page": "html/bg.html",
"content_scripts": [
    {
        "matches": ["\u003Call_urls\u003E"],
        "js": ["js/jquery.js"/]
    }
]

在 bg.html 中,我添加了一个点击事件处理程序来查找

...

节点
<script>

    chrome.browserAction.onClicked.addListener(function(tab) {  
        chrome.tabs.executeScript(null, {code: "alert($('p').text());"});
    });

</script>

只要这样做就可以了。但是如果我在 bg.htm 中添加一个 js 引用,那么 jQuery 就不再工作了,甚至 src=""

<script type="text/javascript" src="../js/jquery.js">

    chrome.browserAction.onClicked.addListener(function(tab) {  
        chrome.tabs.executeScript(null, {code: "alert($('p').text());"});
    });

</script>

background_page 和 content_script 应该在不同的范围内,还是找不到这里发生了什么。

【问题讨论】:

    标签: jquery google-chrome-extension


    【解决方案1】:

    如果您的脚本标签具有src 属性,则内容不会被解析。

    请看这里:http://jsfiddle.net/cnK7s/


    改为使用 2 个单独的 script 标签:

    <script type="text/javascript" src="../js/jquery.js"></script>
    
    <script type="text/javascript">
    
        chrome.browserAction.onClicked.addListener(function(tab) {  
            chrome.tabs.executeScript(null, {code: "alert($('p').text());"});
        });
    
    </script>
    

    【讨论】:

    • 是的,这就是原因。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多