为什么在Firefox中调用这个JQuery函数失败? [英] Why does the call to this JQuery function fail in Firefox?

查看:122
本文介绍了为什么在Firefox中调用这个JQuery函数失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网页上有一段代码链接:

 < a href =javascript:$ ('#comment_form')。toggle('normal')title =对这个帖子的评论> 

这会产生一个应该弹出一个隐藏窗体的链接。它适用于Safari,但在Firefox中,我只是得到一个几乎空白页面,只有以下文本:

  [object对象] 

我确定这与JQuery函数返回的值有关。我不知道如何修改JavaScript函数的调用,所以它也可以在Firefox中使用。

...

 < script type ='text / javascript'> 
jQuery(function($){#文档就绪,在这个范围内访问$作为jQuery
$(a#comment)。click(function(){# ID ='comment'
$('#comment_form')。toggleClass('normal');#在ID注释表单上切换类
返回false;#停止href被跟踪。 b $ b});
});
< / script>
...
< a id =commenthref =/ you_need_javascript_sorry.htmltitle =对此帖子的评论>
[评论]
< / a>

请不要像在HTML中那样嵌入JavaScript。


$ b 如果您将HTML嵌入到HTML中,您可以:


  1. 码。

  2. 有一些奇怪的问题,像是你用黑客来解决它的问题

  3. 尝试中间点击链接的用户将无处可送。

  4. 维护散布在页面链接中的代码的可执行部分将以失败告终。

  5. 当用户没有javascript时,不会优雅地降级

  6. 当您开始需要执行超过2个图层的商店引用深。


I have the following line of code in a link on my webpage:

<a href="javascript:$('#comment_form').toggle('normal')" title="Comment on this post">

This produces a link that should pop open a hidden form. It works on Safari, but in Firefox, I just get an almost-empty page with nothing but the following text:

[object Object]

I'm sure this has something to do with the value returned by the JQuery function, but I'm not sure how to fix the call to the JavaScript function so it works in Firefox, too.

解决方案

For the love of...

<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>

Please, don't embed JavaScript like you just did in the HTML.

If you embed JavaScript in HTML like that, you :

  1. Make messy code.
  2. Have weird problems like you just did with hacks to get around it
  3. Users trying to middle click links will get sent nowhere.
  4. Maintaining the executable part of your code interspersed in page links will end in failure.
  5. Don't gracefully degrade when users don't have javascript
  6. Become problematic when you start needing to do stuff like store quotes more than 2 layers deep.

这篇关于为什么在Firefox中调用这个JQuery函数失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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