最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change the innerHtml content using 'data-id' attribute using jquery? - Stack Overflow

programmeradmin0浏览0评论
<li data-id="528">
  <a title="LOGIN" class="dropdown-toggle" href="#">
          <i class="glyphicon glyphicon-user"></i>LOGIN</a>
  <div style="width: 350px;" data-width="350" data-class="menu-login-row">My other content</div>
</li>

I Have this html I want to change the content by data-id like given below

<li data-id="528">
  <a title="LOGOUT" href="/logout">
          <i class="glyphicon glyphicon-user"></i>LOGOUT</a>
  <div style="width: 350px;" data-width="350" data-class="menu-login-row">My other content</div>
</li>

Is this possible if, Please advice me

Thank you.

<li data-id="528">
  <a title="LOGIN" class="dropdown-toggle" href="#">
          <i class="glyphicon glyphicon-user"></i>LOGIN</a>
  <div style="width: 350px;" data-width="350" data-class="menu-login-row">My other content</div>
</li>

I Have this html I want to change the content by data-id like given below

<li data-id="528">
  <a title="LOGOUT" href="/logout">
          <i class="glyphicon glyphicon-user"></i>LOGOUT</a>
  <div style="width: 350px;" data-width="350" data-class="menu-login-row">My other content</div>
</li>

Is this possible if, Please advice me

Thank you.

Share Improve this question edited Jan 12, 2016 at 5:35 Umashankar Saw asked Jan 12, 2016 at 5:33 Umashankar SawUmashankar Saw 1,1891 gold badge10 silver badges26 bronze badges 3
  • Please be more specific about what you want to acplish, question is quite vague – charlietfl Commented Jan 12, 2016 at 5:36
  • Look at this post: stackoverflow./questions/2487747/… – Thomas Mundal Commented Jan 12, 2016 at 5:37
  • you can get it like $("li[data-id=528]") . – Parth Trivedi Commented Jan 12, 2016 at 5:39
Add a ment  | 

4 Answers 4

Reset to default 4

Yes it's possible.

Like this

var a=$('li[data-id="528"]').find('a');
a.attr('href','/logout');
a.contents().filter(function() {
    return this.nodeType == 3
}).each(function(){
    this.textContent = this.textContent.replace('LOGIN','LOGOUT');
});

You can get the element by attribute selector, and use .html() to modify content:

$('[data-id="528"]').html('<a title="LOGOUT" href="/logout"><i class="glyphicon glyphicon-user"></i>LOGOUT</a><div style="width: 350px;" data-width="350" data-class="menu-login-row">My other content</div>')

Actually you can modify innerHtml of an jquery element by .html()

$(selector).html(whatEverYouWant);

Try it.

var htmlData = '<a title="LOGOUT" href="/logout">';
    htmlData+= '<i class="glyphicon glyphicon-user"></i>LOGOUT</a>';
    htmlData+='<div style="width: 350px;" data-width="350" data-class="menu-login-row">My other content</div>';  


jQuery('li[data-id="528"]').html(htmlData);

You can write just one line code for this

$('li[data-id="528"]').find('a').attr("title","LOGOUT").attr("href","/logout").html('<i class="glyphicon glyphicon-user"></i>LOGOUT</a>');
发布评论

评论列表(0)

  1. 暂无评论