简体   繁体   English

jQuery:DOM元素:属性:查询和创建

[英]jQuery: DOM Elements: Attributes: query & create

I have a few questions about jQuery, relating to attributes: 我对jQuery有一些与属性有关的问题:

  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. 是否可以使用jQuery或DOM API调用来列出或克隆DOM元素的所有属性。 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? 如果知道属性名称,则可以通过jQuery.attr()API调用来执行此操作,但是如果不知道,有没有办法?
  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? 除了明显地动态加载新脚本之外,是否还有一个jQuery或DOM API调用可用于创建新的CSS规则?

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. 这似乎是可能的,因为当我使用CTRL-Shift-J在Google Chrome中打开JS调试器,然后在元素窗格中单击DOM元素时,可以看到该元素的所有属性,而不必按名称要求。

  1. Clone the whole object, that will replicate all attributes as well, http://api.jquery.com/clone/ 克隆整个对象,也将复制所有属性, http://api.jquery.com/clone/
  2. Add new style section into the head element using append, http://api.jquery.com/append/ 使用附加http://api.jquery.com/append/将新样式部分添加到head元素中

The other guys are right that you should use clone - but if you want to list the attributes, you can do it like so: 其他人是对的,您应该使用clone -但是,如果要列出属性,可以这样进行:

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: 我认为您无法创建新的CSS规则,但可以通过将CSS附加到选择器来执行几乎相同的操作:

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

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

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