使链接转到默认 href 之外的其他位置

发布于 2024-12-23 14:54:50 字数 298 浏览 3 评论 0原文

我想抓取链接文本并将其附加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

被翻牌 2024-12-30 14:54:50

您可以通过this访问当前锚点。然后可以通过 this.innerHTML 获取文本。

像这样的东西...

<a href="www.mysite.com/search.aspx?kwd=" onClick="location.href='http://mysite.com/search.aspx?kwd='+ this.innerHTML; return false;">Kangaroo</a>

You can access the current anchor through this. The text can be then had through this.innerHTML.

Something like this...

<a href="www.mysite.com/search.aspx?kwd=" onClick="location.href='http://mysite.com/search.aspx?kwd='+ this.innerHTML; return false;">Kangaroo</a>
从来不烧饼 2024-12-30 14:54:50
$('.your-url').click(function(event) {
    event.preventDefault();
    window.location= $(this).attr('href') + encodeURIComponent($(this).text());
});

我注意到其他答案都没有将链接中的文本编码为查询字符串参数。

内联(如您的示例)看起来像这样:

<a href="www.mysite.com/search.aspx?kwd=" onClick="location.href = this.href + encodeURIComponent($.trim($(this).text()))">Kangaroo</a>

return false 应该是不必要的,因为一旦您更改 location 对象脚本就会停止运行并且页面会发生变化。

更新

您可以使用$.trim()来:

删除字符串开头和结尾的空格。

来源:http://api.jquery.com/jquery.trim/< /em>

$('.your-url').click(function(event) {
    event.preventDefault();
    window.location= $(this).attr('href') + encodeURIComponent($(this).text());
});

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:

<a href="www.mysite.com/search.aspx?kwd=" onClick="location.href = this.href + encodeURIComponent($.trim($(this).text()))">Kangaroo</a>

return false should be unnecessary because once you change the location object scripts stop running and the page changes.

UPDATE

You can use $.trim() to:

Remove the whitespace from the beginning and end of a string.

Source: http://api.jquery.com/jquery.trim/

美人如玉 2024-12-30 14:54:50
$('a.your-url').click(function(e) {
  e.preventDefault();
  url = $(this).attr('href') + $(this).text();
  location.href = url;

});
$('a.your-url').click(function(e) {
  e.preventDefault();
  url = $(this).attr('href') + $(this).text();
  location.href = url;

});
暖伴 2024-12-30 14:54:50

要在单击时将页面发送到链接的 href + text,这应该可行:

$("a").click(function(){
     location.href = $(this).attr("href") + $(this).text();
     return false;
});

但是为什么不在页面加载时正确设置 href,并摆脱所有这些 onclick 完全处理程序?

$("a").each(function(i, el) {
     var $el = $(el);
     $el.attr("href", $el.attr("href") + encodeURI($el.text()));
});

To send the page to the link's href + text when clicked, this should work:

$("a").click(function(){
     location.href = $(this).attr("href") + $(this).text();
     return false;
});

But why not just set the hrefs correctly when the page loads, and get rid of all these onclick handlers altogether?

$("a").each(function(i, el) {
     var $el = $(el);
     $el.attr("href", $el.attr("href") + encodeURI($el.text()));
});
断桥再见 2024-12-30 14:54:50

jQuery 示例:

$('a.link').click(function () {
    var $this = $(this),
        href = $this.attr('href');

    window.location = href + encodeURIComponent($this.text());
    event.preventDefault();
    return false;
});

演示

jQuery example:

$('a.link').click(function () {
    var $this = $(this),
        href = $this.attr('href');

    window.location = href + encodeURIComponent($this.text());
    event.preventDefault();
    return false;
});

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文