将自定义属性附加到DOM节点 [英] Append custom attributes to a DOM node

查看:110
本文介绍了将自定义属性附加到DOM节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我可以在HTML标签上添加属性吗?




我不知道我问的是甚么可能,但我希望能够向现有的HTML DOM节点添加自定义(非渲染)属性。



例如,如果我有一个简单的DOM,如下所示:

 <身体GT; 
< p>
< span id =aSpan>< / span>
< / p>
< / body>

..我希望能够将自定义属性添加到跨度'aSpan'到存储一个数字变量。



这是否可能,如果是,最好的方法是什么?



解决方案

当然,我一直都这样做。你可以在html中执行:



< span id =aSpanattname =attvalue>



(验证器不喜欢这个,但技术上它不是有效的HTML,但它的工作)



或通过javascript:



element.setAttribute('attname','attvalue');



您可以阅读:



element.getAttribute('attname');


Possible Duplicate:
Can I just make up attributes on my HTML tags?

Hi,

I am not sure if what I am asking is even possible, but I'd like to be able to add a custom (nonrendered) propery to an existing HTML DOM node.

For example, if I have a simple DOM as below:

<body>
 <p>
  <span id="aSpan"></span>
 </p>
</body>

.. I'd like to be able to add a custom property to the span 'aSpan' to store a numeric variable.

Is this possible and if so, what is the best way to do it?

Thanks,

解决方案

Sure, I do this all the time. You can do it in the html:

<span id="aSpan" attname="attvalue">

(validators don't like this though, technically it's not valid html but it works)

Or via javascript:

element.setAttribute('attname', 'attvalue');

You can read it with:

element.getAttribute('attname');

这篇关于将自定义属性附加到DOM节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆