简体   繁体   中英

jQuery: DOM Elements: Attributes: query & create

I have a few questions about jQuery, relating to attributes:

  1. Is there a jQuery or DOM API call that I could use to list or clone all of the attributes of a DOM element. The jQuery.attr() API call lets you do it if you know the name of the attribute, but if you don't, is there a way?
  2. Is there a jQuery or DOM API call that I could use to create a new CSS rule, besides the obvious one of dynamically loading a new script?

It seems possible because when I open up the JS debugger in Google Chrome using CTRL-Shift-J, and click on a DOM element in the elements pane, I can see all of the attributes of the element without having to ask for them by name.

  1. Clone the whole object, that will replicate all attributes as well, http://api.jquery.com/clone/
  2. Add new style section into the head element using append, http://api.jquery.com/append/

The other guys are right that you should use clone - but if you want to list the attributes, you can do it like so:

for(attr in $('#selector')) { 
    console.log(attr);
}

I don't think you can create a new css rule, but you can do pretty much the same by attaching css to a selector:

$('#selector').css({
    display: 'block'
});

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