使链接转到默认 href 之外的其他位置
我想抓取链接文本并将其附加到 URL 并打开新 URL,并在原始链接的 Onclick 中添加查询字符串。如何使用 javascript 或 jquery 获取链接文本?
<a href="www.mysite.com/search.aspx?kwd=" onClick="location.href='http://mysite.com/search.aspx?kwd='+ Grab text 'kangaroo' and append here as QueryString>Kangaroo</a>
I want to grab the link text and append it to the URL and open the new URL with querystring added Onclick of the Original Link..How do I get the link text using javascript or jquery?
<a href="www.mysite.com/search.aspx?kwd=" onClick="location.href='http://mysite.com/search.aspx?kwd='+ Grab text 'kangaroo' and append here as QueryString>Kangaroo</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过
this
访问当前锚点。然后可以通过this.innerHTML
获取文本。像这样的东西...
You can access the current anchor through
this
. The text can be then had throughthis.innerHTML
.Something like this...
我注意到其他答案都没有将链接中的文本编码为查询字符串参数。
内联(如您的示例)看起来像这样:
return false
应该是不必要的,因为一旦您更改location
对象脚本就会停止运行并且页面会发生变化。更新
您可以使用
$.trim()
来:来源:http://api.jquery.com/jquery.trim/< /em>
I noticed that none of the other answers were encoding the text in the link to be a query-string parameter.
Inline (like your example) would look like this:
return false
should be unnecessary because once you change thelocation
object scripts stop running and the page changes.UPDATE
You can use
$.trim()
to:Source: http://api.jquery.com/jquery.trim/
要在单击时将页面发送到链接的
href + text
,这应该可行:但是为什么不在页面加载时正确设置 href,并摆脱所有这些
onclick
完全处理程序?To send the page to the link's
href + text
when clicked, this should work:But why not just set the hrefs correctly when the page loads, and get rid of all these
onclick
handlers altogether?jQuery 示例:
演示
jQuery example:
Demo