admin管理员组文章数量:1134739
I am using The DynaTree () but I am having some problems and hoping someone can help..
I am displaying the tree on the page like below:
<div id="tree">
<ul>
<li class="folder">Outputs
<ul>
<li id="item1" data="icon: 'base.gif', url: 'page1.htm', target: 'AccessPage'">Item 1 Title</li>
<li id="item2" data="icon: 'base.gif', url: 'page2.htm', target: 'AccessPage'">Item 2 Title</li>
<li id="item3" data="icon: 'base.gif', url: 'page3.htm', target: 'AccessPage'">Item 3 Title</li>
<li id="item4" data="icon: 'base.gif', url: 'page4.htm', target: 'AccessPage'">Item 4 Title</li>
</ul>
</li>
</ul>
</div>
However I am trying to change the icon on a item no matter if it's selected or not only using JavaScript.
the new icon I want to use is base2.gif
I have tried using the following but it don't seem to work:
document.getElementById('item1').data = "icon: 'base2.gif', url: 'output.htm', target: 'AccessPage', output: '1'";
anyone know what I might be doing wrong?
I am using The DynaTree (https://code.google.com/p/dynatree) but I am having some problems and hoping someone can help..
I am displaying the tree on the page like below:
<div id="tree">
<ul>
<li class="folder">Outputs
<ul>
<li id="item1" data="icon: 'base.gif', url: 'page1.htm', target: 'AccessPage'">Item 1 Title</li>
<li id="item2" data="icon: 'base.gif', url: 'page2.htm', target: 'AccessPage'">Item 2 Title</li>
<li id="item3" data="icon: 'base.gif', url: 'page3.htm', target: 'AccessPage'">Item 3 Title</li>
<li id="item4" data="icon: 'base.gif', url: 'page4.htm', target: 'AccessPage'">Item 4 Title</li>
</ul>
</li>
</ul>
</div>
However I am trying to change the icon on a item no matter if it's selected or not only using JavaScript.
the new icon I want to use is base2.gif
I have tried using the following but it don't seem to work:
document.getElementById('item1').data = "icon: 'base2.gif', url: 'output.htm', target: 'AccessPage', output: '1'";
anyone know what I might be doing wrong?
Share Improve this question edited May 25, 2023 at 21:06 Clint Warner 1,2651 gold badge10 silver badges25 bronze badges asked Jul 2, 2012 at 0:20 AaronAaron 3,62913 gold badges36 silver badges49 bronze badges 1 |3 Answers
Reset to default 264Use the setAttribute
method:
document.getElementById('item1').setAttribute('data', "icon: 'base2.gif', url: 'output.htm', target: 'AccessPage', output: '1'");
But you really should be using data followed with a dash and with its property, like:
<li ... data-icon="base.gif" ...>
And to do it in JS use the dataset
property:
document.getElementById('item1').dataset.icon = "base.gif";
Please use dataset
var article = document.querySelector('#electriccars'),
data = article.dataset;
// data.columns -> "3"
// data.indexnumber -> "12314"
// data.parent -> "cars"
so in your case for setting data:
getElementById('item1').dataset.icon = "base2.gif";
For people coming from Google, this question is not about data attributes - OP added a non-standard attribute to their HTML object, and wondered how to set it.
However, you should not add custom attributes to your properties - you should use data attributes - e.g. OP should have used data-icon
, data-url
, data-target
, etc.
In any event, it turns out that the way you set these attributes via JavaScript is the same for both cases. Use:
ele.setAttribute(attributeName, value);
to change the given attribute attributeName
to value
for the DOM element ele
.
For example:
document.getElementById("someElement").setAttribute("data-id", 2);
Note that you can also use .dataset
to set the values of data attributes, but as @racemic points out, it is 62% slower (at least in Chrome on macOS at the time of writing). So I would recommend using the setAttribute
method instead.
本文标签: domSet custom attribute using JavaScriptStack Overflow
版权声明:本文标题:dom - Set custom attribute using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736800684a1953478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
data
is a prefix. You should be usingdata-you-attribute-name
– MilkyWayJoe Commented Jul 2, 2012 at 0:28