[英]capture link text and append to the URL as query string
I want to capture the text that is in between anchor link tag and pass it to the href value as query string so it looks like http://mysite.com/events/Pages/default.aspx?cat=cancer 我想捕获锚链接标记之间的文本,并将其作为查询字符串传递给href值,使其看起来像http://mysite.com/events/Pages/default.aspx?cat=cancer
The reason I can't add that manually is because the value in between and is dynamic. 我无法手动添加的原因是因为它之间的值是动态的。 How do I capture that and append to the url using jquery or javascript??
我如何捕获它并使用jquery或javascript追加到url?
or i can maybe, at the event of Cancer link being clicked, direct it to http://mysite.com/events/Pages/default.aspx?cat=cancer 或者我可以,在点击巨蟹座链接的情况下,将其指向http://mysite.com/events/Pages/default.aspx?cat=cancer
<a href="http://mysite.com/events/Pages/default.aspx?cat="> Cancer</a>
$("a").on("click", function (e) {
e.preventDefault();
window.location = this.href + $.trim($(this).text());
});
Or you could replace the href
attribute for each link: 或者,您可以为每个链接替换
href
属性:
$("a").prop("href", function () {
return this.href += $.trim($(this).text());
});
Then clicking each link will automatically direct the user correctly. 然后单击每个链接将自动正确定向用户。 Your selector (
$("a")
should be more specific, depending on your markup) 您的选择器(
$("a")
应该更具体,具体取决于您的标记)
Example: http://jsfiddle.net/6U749/ 示例: http : //jsfiddle.net/6U749/
Edit: If you have to do it inline, here's one way: 编辑:如果你必须内联,这是一种方式:
<a href="http://mysite.com/events/Pages/default.aspx?cat=" onclick="window.location.href = this.href + $.trim($(this).text());"> Cancer</a>
You could do something like this: 你可以这样做:
$('.someClassImStickingOnLinksThatNeedThisBehavior').each(function()
{
this.href = $.trim(this.href) + $.trim(this.innerHTML);
});
Then, for any link, it would automatically update the HREF to the current HREF plus the value of the node. 然后,对于任何链接,它会自动将HREF更新为当前HREF加上节点的值。
You can do: 你可以做:
var yourlink = $("#youlink");
var href = yourlink.attr("href");
var text = yourlink.html();
yourlink.attr("href", href + "?cat=" + text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.