this.id (as you know)
this.value (on most input types. only issues I know are IE when a <select> doesn't have value properties set on its <option> elements, or radio inputs in Safari.)
this.className to get or set an entire "class" property
this.selectedIndex against a <select> to get the selected index
this.options against a <select> to get a list of <option> elements
this.text against an <option> to get its text content
this.rows against a <table> to get a collection of <tr> elements
this.cells against a <tr> to get its cells (td & th)
this.parentNode to get a direct parent
this.checked to get the checked state of a checkbox Thanks @Tim Down
this.selected to get the selected state of an option Thanks @Tim Down
this.disabled to get the disabled state of an input Thanks @Tim Down
this.readOnly to get the readOnly state of an input Thanks @Tim Down
this.href against an <a> element to get its href
this.hostname against an <a> element to get the domain of its href
this.pathname against an <a> element to get the path of its href
this.search against an <a> element to get the querystring of its href
this.src against an element where it is valid to have a src
...我想你已经明白了。
有时,性能是至关重要的。比如,如果你在循环中多次执行某项操作,你可能想要抛弃jQuery。
一般来说,你可以替换:
$(el).attr('someName');
:
上面的措辞很糟糕。getAttribute不是一个替换,但它检索从服务器发送的属性的值,其对应的setAttribute将设置它。在某些情况下是必要的。
下面的句子在某种程度上涵盖了它。请看这个答案,了解更好的治疗方法。
el.getAttribute('someName');
...以便直接访问属性。注意,属性与属性不同(尽管它们有时相互镜像)。当然还有setAttribute。
假设您遇到了这样一种情况:收到了一个页面,需要打开某种类型的所有标记。它是简单的jQuery:
$('span').unwrap(); // unwrap all span elements
但如果有很多,你可能想要做一点本地DOM API:
var spans = document.getElementsByTagName('span');
while( spans[0] ) {
var parent = spans[0].parentNode;
while( spans[0].firstChild ) {
parent.insertBefore( spans[0].firstChild, spans[0]);
}
parent.removeChild( spans[0] );
}
这段代码非常短,它的性能比jQuery版本更好,并且可以很容易地在你的个人库中成为一个可重用的函数。
因为while(span[0]),它看起来像是一个外部while的无限循环,但因为我们处理的是一个“活动列表”,当我们执行parent. removecchild (span[0]);时,它会被更新。这是我们在使用数组(或类数组对象)时忽略的一个非常漂亮的特性。