mip*_*adi 5 javascript firefox jquery
我在我的网页上的链接中有以下代码行:
<a href="javascript:$('#comment_form').toggle('normal')" title="Comment on this post">
Run Code Online (Sandbox Code Playgroud)
这会生成一个应该弹出隐藏表单的链接.它适用于Safari,但在Firefox中,我只得到一个几乎为空的页面,只有以下文字:
[object Object]
Run Code Online (Sandbox Code Playgroud)
我确定这与jQuery函数返回的值有关,但我不确定如何修复对JavaScript函数的调用,因此它也适用于Firefox.
Ken*_*ric 21
为了爱...
<script type='text/javascript'>
jQuery(function($){ # Document ready, access $ as jQuery in this scope
$("a#comment").click(function(){ # bind a click event on the A like with ID='comment'
$('#comment_form').toggleClass('normal'); # Toggle the class on the ID comment form
return false; # stop the href from being followed.
});
});
</script>
....
<a id="comment" href="/you_need_javascript_sorry.html" title="Comment on this post">
[Comment]
</a>
Run Code Online (Sandbox Code Playgroud)
请不要像在HTML中那样嵌入JavaScript.
如果你像这样在HTML中嵌入JavaScript,你:
尝试:
<a href="javascript:void($('#comment_form').toggle('normal'))" title="Comment on this post">
Run Code Online (Sandbox Code Playgroud)
在 a 中包含脚本void()将禁止浏览器显示执行结果。
更新
我直接回答了最初的问题,用最省力的解决方案。正如这里的其他一些答案中提到的那样,我个人会将我的标记和 JavaScript 分开并动态添加onclick处理程序,而不是将脚本嵌入到href属性中。
| 归档时间: |
|
| 查看次数: |
5304 次 |
| 最近记录: |