Chrome扩展与jQuery [英] Chrome extensions with jQuery

查看:80
本文介绍了Chrome扩展与jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了 http://code.google.com/chrome/extensions/ samples.html#ea2894c41cb8e80a4433a3e6c5772dadce9be90d
我想让它成为jQuery,但是如果我这样做:

I downloaded http://code.google.com/chrome/extensions/samples.html#ea2894c41cb8e80a4433a3e6c5772dadce9be90d. I would like make it it jQuery, but if i do:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>

<script>
$("div").css('background-color', 'black');

$('.click').click(function(){
  chrome.tabs.executeScript(null,
    {code:"document.body.style.backgroundColor='" + $(this).attr('id') + "'"});
  window.close();
})
</script>
<div class="click" id="red">red</div>
<div class="click" id="blue">blue</div>
<div class="click" id="green">green</div>
<div class="click" id="yellow">yellow</div>

这不起作用。什么都没发生。为什么?

this not working. Nothing happens. Why?

推荐答案

您没有包含文档就绪处理程序,请尝试以下操作:

You didn't include the document ready handler, try this:

<script>
    $(function() {
        $("div").css('background-color', 'black');

        $('.click').click(function() {
            chrome.tabs.executeScript(null,
                {code:"document.body.style.backgroundColor='" + $(this).attr('id') + "'"});
            window.close();
        })
    });
</script>
<div class="click" id="red">red</div>
<div class="click" id="blue">blue</div>
<div class="click" id="green">green</div>
<div class="click" id="yellow">yellow</div>

您也可以将< script> 标记添加到< / body> 标记之前,以便所有HTML都在javascript之前加载。

Alternatively you can move your <script> tag to just before the </body> tag so that all HTML is loaded before the javascript.

这篇关于Chrome扩展与jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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