how to change text value within an element?

后端 未结 4 745
野的像风
野的像风 2021-01-01 18:36

How do you change the text for all within the a to

continue reading

using jquery

4条回答
  •  抹茶落季
    2021-01-01 19:19

    Do it with jQuery inside of a document ready handler ($(fn))...

    $('.post-read a').text('continue reading');
    

    jsFiddle.

    For the sake of it, here is how to do it without jQuery....

    var anchor = document.getElementsByClassName('post-read')[0].getElementsByTagName('a')[0],
        textProperty;
    
    if (anchor.textContent) {
        textProperty = 'textContent';
    } else if (anchor.innerText) {
        textProperty = 'innerText';
    }
    anchor[textProperty] = 'continue reading';
    

    jsFiddle.

    This will work good for your piece of HTML, but it isn't too generic.

    If you don't care about setting innerText property, you could use...

    anchor.textContent = anchor.innerText = 'continue reading';
    

    I wouldn't recommend it though.

提交回复
热议问题