使用 jQuery 在 Chrome 扩展弹出窗口中显示隐藏 div
我一直在尝试在浏览器操作弹出窗口中显示和隐藏(可能切换)选项,但没有成功。 下面的代码是我的弹出窗口的主体
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
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 tojQuery()
should solve the issue.由于您既没有从点击处理程序返回 false,也没有调用
preventDefault()
,因此页面将在点击时重新加载,并且.somecontent
将再次隐藏。将您的 JS 更改为以下内容: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:可能 popup.js 导致解析/执行错误?尝试看看 chrome 是否可以打印错误:
来自此处,
如果 jquery 不存在,则可能会发生错误t已加载:
它应该弹出该对象...
probably popup.js is causing a parse/exec error? try seeing if chrome can print errors with:
from here
that error you got could happen if jquery isn't loaded:
it should popup the object...
该错误意味着 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.
你的代码对我来说效果很好。正如其他人所说,您收到的错误听起来像是未找到/加载 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.
由于这个老问题似乎仍未得到解答,我想我会试试运气。
您是否尝试再次下载 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/)