【问题标题】:Setting element href value jQuery设置元素href值jQuery
【发布时间】:2017-07-05 19:59:06
【问题描述】:

已经有一些关于此的帖子,this one 再简单不过了。出于某种原因,虽然我无法让元素链接到 href 属性。

这是需要链接的元素的类:.vc_tta-tab:nth-child(1);

页面上有一个锚点#about;

这是我正在使用的 jQuery:

<script>
jQuery('.vc_tta-tab:nth-child(1)').prop('href','https://example.com/#about');
</script>

这个想法是使用.vc_tta-tab:nth-child(1) 元素链接到页面上的/#about 部分。

请有人帮我纠正我做错了什么?

【问题讨论】:

  • 使用attr 代替prop
  • @RAUSHANKUMAR 谢谢,我试过了,不幸的是它也不起作用
  • 在什么情况下它不起作用?您遇到了哪些控制台错误?
  • @jmargolisvt 我收到了Uncaught Error: Syntax error, unrecognized expression: #,请看这里:s23.postimg.org/45fww8kaj/…
  • 那张截图没有用...您需要提供更多代码,此代码不是这里的问题...这工作正常

标签: javascript jquery attributes


【解决方案1】:

VDesign 使用attr 代替prop 是正确的。对于您的 URL,我认为您可能需要实际页面才能使用锚点。喜欢http://example.com/index.html#about 而不是http://example.com/#about

【讨论】:

  • 它甚至可以使用 prop... 问题出在他的代码中的其他地方,这里是 prop jsfiddle.net/fxabnk4o/9 的工作小提琴
  • 啊,确实如此。我试图找出他在另一个线程中的语法错误来自哪里(我还没有代表评论其他人的答案)。
猜你喜欢
  • 2013-06-08
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-19
  • 1970-01-01
相关资源
最近更新 更多