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

javascript - Changing data-content of a button class used in popover - Stack Overflow

programmeradmin4浏览0评论

I have a button class which I am using for twitter popover, my code is as follow:

<button class="btn btn-success" id="chaticon" data-original-title="Users Online" data-content="&lt;a href='#'&gt; OMGTEST &lt;/a&gt;">

And what I want to do is to modify the data-content via javascript,

Naively I tried to do:

document.getElementById("chaticon").data-content = "new content";

and it didn't work, any ideas on how I can do this?

I have a button class which I am using for twitter popover, my code is as follow:

<button class="btn btn-success" id="chaticon" data-original-title="Users Online" data-content="&lt;a href='#'&gt; OMGTEST &lt;/a&gt;">

And what I want to do is to modify the data-content via javascript,

Naively I tried to do:

document.getElementById("chaticon").data-content = "new content";

and it didn't work, any ideas on how I can do this?

Share Improve this question edited Mar 3, 2013 at 21:00 APerson 8,4208 gold badges38 silver badges49 bronze badges asked Mar 3, 2013 at 20:58 user1360113user1360113
Add a ment  | 

2 Answers 2

Reset to default 4

Use the built in accessors for HTMLElement.

getAttribute(attributeName)
setAttribute(attributeName,newValue);

like this:

var yourElement = document.getElementById("chaticon");
var dataVal = yourElement.getAttribute("data-content");
var newData = "new data";
yourElement.setAttribute("data-content",newData);

here is a simple demo: http://jsfiddle/hpfk3/

edit

You should probably have included jquery and popover in the question instead of just asking about a button element. Since these are available, you can change the content like this:

//store chat icon jQuery object
var chatIcon = $("#chaticon");

//change data attribute on element
//change jquery data object content
//call setcontent method
chatIcon.attr("data-content","new data").data('popover').setContent();

//target the popover element and restore its placement definition
chatIcon.data('popover').$tip.addClass(chatIcon.data('popover').options.placement);

Try setting the attribute

document.getElementById("chaticon").setAttribute('data-content','new content');
发布评论

评论列表(0)

  1. 暂无评论