简体   繁体   English

更改弹出窗口中使用的按钮类的数据内容

[英]Changing data-content of a button class used in popover

I have a button class which I am using for twitter popover, my code is as follow: 我有一个按钮类,我用于twitter popover,我的代码如下:

<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, 我想要做的是通过javascript修改data-content

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? 它没有用,关于我怎么做的任何想法?

Use the built in accessors for HTMLElement. 使用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.net/hpfk3/ 这是一个简单的演示: http//jsfiddle.net/hpfk3/

edit 编辑

You should probably have included jquery and popover in the question instead of just asking about a button element. 您可能应该在问题中包含jquery和popover,而不是只询问按钮元素。 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);

尝试设置属性

document.getElementById("chaticon").setAttribute('data-content','new content');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM