[英]Change href of a link jquery
currently I have a textbox in which someone can type in a link, and then a link on the page next to the textbox should change it's href attribute to the text the user just typed in. 当前,我有一个文本框,有人可以在其中输入链接,然后该文本框旁边的页面上的链接应将其href属性更改为用户刚刚输入的文本。
My javascript: 我的JavaScript:
var LinkText = $("[id$=TextBox]").val();
$("[id$=DocumentLink]").href = LinkText;
My HTML: 我的HTML:
<a id ="DocumentLink" target = "_blank" href="http://www.currentlink.com/">Link to Document</a>
<input id="TextBox" type="text" /> `
Although LinkText is picked up as the string typed in the textbox, the second line of my javascript is not working as I want. 尽管选择了LinkText作为在文本框中键入的字符串,但是我的javascript的第二行无法按我的要求工作。 The link stays as the currentlink.
链接保持为当前链接。 I have jQuery 1.4.2 if that helps, I could be doing something that doesn't work with that maybe.
我有jQuery 1.4.2,如果有帮助的话,我可能正在做一些可能不起作用的事情。
Since you have a jQuery object you have to set the attr
由于您有一个jQuery对象,因此必须设置
attr
$("[id$=DocumentLink]").attr("href", LinkText);
Or you can get the actual HTMLElement at the 0
index and call .href
that way: 或者,您可以在
0
索引处获得实际的HTMLElement并以这种方式调用.href
:
$("[id$=DocumentLink]")[0].href = LinkText;
And since your matching an exact ID, just use $("#DocumentLink")
并且由于您匹配的是准确的ID,因此只需使用
$("#DocumentLink")
Try it: 试试吧:
$("#DocumentLink").attr('href', LinkText);
Regards. 问候。
jQuery object doesn't have href
property. jQuery对象没有
href
属性。 You are defining a property for the jQuery object which doesn't affect the href
property of the HTMLElement object in the jQuery collection. 您正在为jQuery对象定义一个属性,该属性不会影响jQuery集合中HTMLElement对象的
href
属性。 You could use the attr
or the prop
method instead. 您可以改用
attr
或prop
方法。
set the attr of the link. 设置链接的属性。
$("[id$=DocumentLink]").attr("href", LinkText);
i think it would be much easier to use JS and innerHTML
. 我认为使用JS和
innerHTML
会容易得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.