【问题标题】:jQuery IF anchor source is not hash thenjQuery IF 锚源不是散列然后
【发布时间】:2012-03-22 00:10:38
【问题描述】:

我有一堆图像包裹在虚拟锚点中,它们的源设置为#。我正在编写一个函数来检查源是否不是散列并给它一些特殊的东西。好像有什么不对劲..帮忙?

http://jsfiddle.net/danielredwood/zR95t/

if (!$(this).attr('href', '#')) {
    //blahblah not a hash
} else {
    //do some other thing
}

【问题讨论】:

  • 你小提琴中的代码没有意义。它什么时候被调用,是什么调用它? $(this) 指的是什么?您在不是事件侦听器的东西上调用 event.preventDefault()。
  • 复制/粘贴错误。jsfiddle.net/danielredwood/zR95t/1 你不应该对这个问题投反对票,就好像它不是以前没有在网站上问过的合法问题一样。

标签: jquery hash if-statement anchor


【解决方案1】:

应该是:

if ($(this).attr('href') != '#') {
    //blahblah not a hash
} else {
    //do some other thing
}

否则您将设置 '#' 作为 href 属性,请参阅:http://api.jquery.com/attr/

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 2011-05-19
相关资源
最近更新 更多