使用 jQuery 在 Chrome 扩展弹出窗口中显示隐藏 div

发布于 2024-08-18 06:39:45 字数 1098 浏览 2 评论 0原文

我一直在尝试在浏览器操作弹出窗口中显示和隐藏(可能切换)选项,但没有成功。 下面的代码是我的弹出窗口的主体

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="popup.js"></script>
<div class="show">
    <a href="#" class="showcontent">Show</a>
    <a href="#" class="hidecontent">Hide</a>
    <div class="somecontent">
        <p>some content<br />
            <a href="#">Link</a><br />
        </p>
    </div>
</div>

popup.js 文件包含以下内容:

$(document).ready(function(){
    $(".somecontent").hide();
    $(".showcontent").click(function(){
        $(".somecontent").show();
    });

    $(".hidecontent").click(function(){
        $(".somecontent").hide();
    });
});

我相信问题是 Chrome API 与我的 popup.js 文件存在问题。正文出现在我的弹出窗口中,但“显示”和“隐藏”操作不起作用。有什么想法如何使这项工作有效,如果没有,有另一种方法可以得到相同的结果(即:点击切换)?

编辑:从 javascript 控制台,我收到此错误:

未捕获类型错误:无法调用 null 的“ready”方法

它指向上面使用ready函数的代码行。

I've been trying to make an option show and hide(possibly toggle) in my browser action popup without success.
The code below is the body of my popup

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="popup.js"></script>
<div class="show">
    <a href="#" class="showcontent">Show</a>
    <a href="#" class="hidecontent">Hide</a>
    <div class="somecontent">
        <p>some content<br />
            <a href="#">Link</a><br />
        </p>
    </div>
</div>

The popup.js file contains, among other things:

$(document).ready(function(){
    $(".somecontent").hide();
    $(".showcontent").click(function(){
        $(".somecontent").show();
    });

    $(".hidecontent").click(function(){
        $(".somecontent").hide();
    });
});

I believe the problem is that the Chrome API is having problems with my popup.js file. The body appears in my popup but the Show and Hide actions do not work. Any ideas how to make this work and if not, another way to get the same result (ie: toggle on click)?

EDIT: From the javascript Console, the error I'm getting this error:

Uncaught TypeError: Cannot call method 'ready' of null

Which points to the line of the above code using the ready function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

﹏雨一样淡蓝的深情 2024-08-25 06:39:46

jQuery 也可能与页面中使用 $() 函数的其他内容发生冲突。 (我自己刚刚遇到了这个问题。)如果是这种情况,将 $() 调用更改为 jQuery() 应该可以解决问题。

It's also possible that jQuery is conflicting with something else in your page for the use of the $() function. (I just ran into this problem myself.) If that's the case, changing the $() calls to jQuery() should solve the issue.

数理化全能战士 2024-08-25 06:39:46

由于您既没有从点击处理程序返回 false,也没有调用 preventDefault(),因此页面将在点击时重新加载,并且 .somecontent 将再次隐藏。将您的 JS 更改为以下内容:

$(document).ready(function(){
    $(".somecontent").hide();
    $(".showcontent").click(function(){
        $(".somecontent").show(); return false;
    });

    $(".hidecontent").click(function(){
        $(".somecontent").hide(); return false;
    });
});

Since you are neither returning false from your click handlers nor calling preventDefault(), the page will get reloaded on clicking and the .somecontent will be hidden again. Change your JS to following:

$(document).ready(function(){
    $(".somecontent").hide();
    $(".showcontent").click(function(){
        $(".somecontent").show(); return false;
    });

    $(".hidecontent").click(function(){
        $(".somecontent").hide(); return false;
    });
});
绮筵 2024-08-25 06:39:46

可能 popup.js 导致解析/执行错误?尝试看看 chrome 是否可以打印错误:

JavaScript 控制台:单击页面
菜单图标并选择开发人员 -
JavaScript 控制台。从这里开始,你将
能够查看错误
JavaScript执行,并输入
额外的 JavaScript 命令
执行。

JavaScript 调试器:作为页面提供
菜单图标 0 开发人员 - 调试
JavaScript 调试器提供了
您可以从中设置命令提示符
断点、回溯等等。类型
调试器命令行的帮助
开始吧。

来自此处

如果 jquery 不存在,则可能会发生错误t已加载:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

<script>alert($(document))</script>

它应该弹出该对象...

probably popup.js is causing a parse/exec error? try seeing if chrome can print errors with:

JavaScript Console: click the Page
menu icon and select Developer -
JavaScript Console. From here, you'll
be able to view errors in the
JavaScript execution, and enter
additional JavaScript commands to
execute.

JavaScript Debugger: available as Page
menu icon 0 Developer - Debug
JavaScript, the debugger provides a
command prompt from which you can set
breakpoints, backtrace, and more. Type
help at the debugger command line to
get started.

from here

that error you got could happen if jquery isn't loaded:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

<script>alert($(document))</script>

it should popup the object...

月牙弯弯 2024-08-25 06:39:46

该错误意味着 jQuery 未正确加载。检查并确保它名为 jquery.js 并存储在根文件夹中。

That error means jQuery is not being loaded properly. Check to make sure it's called jquery.js and stored in the root folder.

迷路的信 2024-08-25 06:39:46

你的代码对我来说效果很好。正如其他人所说,您收到的错误听起来像是未找到/加载 jquery.js 文件。
这是一个不太可能的情况,但如果您使用的是 Windows,请检查 jquery.js 文件的属性,并查看第一个选项卡上是否有一个取消阻止按钮。单击它。

Your code worked fine for me. As other said the error you're getting sounds like the jquery.js file isn't being found/loaded.
This is a long shot, but if you're on Windows check the properties of the jquery.js file and see if there's an Unblock button right on the first tab. Click it.

缘字诀 2024-08-25 06:39:46

由于这个老问题似乎仍未得到解答,我想我会试试运气。

您是否尝试再次下载 jquery.js(文件可能已损坏)。另外,如果您仍然有兴趣解决它,您可以将您的 src 发布到某个地方供我们阅读(例如 http://jsfiddle.net /)

As it seems this old question is still not answered I thought I'd try my luck.

Have you tried downloading jquery.js again (file might be corrupted). Also, if you are still interested in solving it, can you post your src somewhere for us to read (like http://jsfiddle.net/)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文