javascript 如何使用 jQuery 更改链接的标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7226592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I change the title of a link using jQuery
提问by JZ.
Here is the code I have:
这是我的代码:
$('#link').attr("href",link)
$('#link').text(text)
How do I change the title of a link using jQuery? I'm correctly changing the url, but I can't edit the text, what am I doing wrong?
如何使用 jQuery 更改链接的标题?我正确地更改了 url,但我无法编辑文本,我做错了什么?
<a id="link" href="" target="_blank">text</a>
$('#link').attr("href",data[1].url)
$('#link').attr("title",data[1].title)
<a href="http://link.com">title</a>
I'm tring to simply change 2 things:
我试图简单地改变两件事:
- url
- title (as show above)
- 网址
- 标题(如上所示)
I'm able to change the link, but the title won't change. I'm selecting the wrong trhing. Therefor is there a way to list all attr available to me? Or are you able to help me change the text title above?
我可以更改链接,但标题不会更改。我选择了错误的 tring。因此,有没有办法列出我可用的所有属性?或者你能帮我改一下上面的文字标题吗?
Either answer is acceptable.
任何一个答案都可以接受。
<div id="highlight" class="topicHighlight hero1">
<h3 id="h3">hero_1_large_text</h3>
<p id="p"></p>
<span id="coverTextSpan">hero_1_small_text</span>
<a id="link" href="url" target="_blank">text</a>
</div>
回答by Alex Pacurar
回答by Jay
回答by Chandresh M
you can try this javascript only.
你只能试试这个javascript。
document.getElementById('link').innerHTML = "new title";
i think this will surly helpful to you..
我认为这对你很有帮助..
Thanks.
谢谢。
回答by Shedokan
If all these answers don't work for you then you should check:
如果所有这些答案都不适合您,那么您应该检查:
- What element are you selecting and see if it's the correct one
- What variable are you assigning to the title(data[1].title)
- 你在选择什么元素,看看它是否正确
- 您将什么变量分配给 title(data[1].title)
I recommend using firebug in firefox or using the dev console in google chrome, anduse the console.log() function to log things so that you don't have to alert() them all the time.
我建议在 firefox 中使用 firebug 或在 google chrome 中使用开发控制台,并使用 console.log() 函数来记录事情,这样你就不必一直提醒它们。