How do I change the title of a link using jQuery

孤人 提交于 2019-12-10 11:25:51

问题


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?

<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:

  1. url
  2. 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?

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>

回答1:


use html function:

$('#link').html(text);

or , if you are talking about the title attribute:

$('#link').attr('title','some title');



回答2:


I think you need to use the HTML() method to change the content of the anchor tag. Here is the link to the documentation.




回答3:


you can try this javascript only.

document.getElementById('link').innerHTML = "new title";

i think this will surly helpful to you..

Thanks.




回答4:


If all these answers don't work for you then you should check:

  1. What element are you selecting and see if it's the correct one
  2. What variable are you assigning to the 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.



来源:https://stackoverflow.com/questions/7226592/how-do-i-change-the-title-of-a-link-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!