How to change only text node in element

前端 未结 8 776
不知归路
不知归路 2020-11-28 10:40

I have next html:


And I want to chang

相关标签:
8条回答
  • 2020-11-28 11:02

    This is the solution that worked for the most browsers

    $('label[for="user_name"]').contents().last()[0].nodeValue = 'Title';
    

    This one came close but gave issues in ie8 since textContent is not supported

    $('label[for="user_name"]').contents().last()[0].textContent='Title';
    
    0 讨论(0)
  • 2020-11-28 11:04

    You can select only the abbr element, store it, and then replace the whole content with the stored element plus the changed caption:

    ​$('label[for="user_name"]').each(function(){
       var a = $(this).children('abbr');
       $(this).html(a).append('Title');
    });
    

    See this fiddle

    0 讨论(0)
提交回复
热议问题