简体   繁体   中英

Hide attribute of an element from DOM

As the title says, I would like to add a new hidden attribute to an element. To avoid any confusion, I want the attribute to be hidden, not the element.

I have a div: <div id="hello_div">Hello!</div>

I add an attribute: $('#hello_div').attr('my_attr', 'attr_value');

So I can get it later: var v = $('#hello_div').attr('my_attr');

It works as expected but if you inspect the div in the DOM Explorer, you see:

<div id="hello_div" my_attr="attr_value">Hello!</div>

Is there any way to hide this attribute?

You can't hide DOM-attributes, but as I see you are using jQuery, you could try data . Eg

$('#hello_div').data('my_attr', 'attr_value');

and later retrieve:

var v = $('#hello_div').data('my_attr');

When you get back your required attribute var v = $('#hello_div').attr('my_attr');

remove the attribute after this $('#hello_div').removeAttr('my_attr');

否。如果您通过添加属性来操作DOM,则在使用Chrome开发人员工具等DOM检查器时,该属性将始终可见。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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