xml parse required attr?! prop?

Try this at your JavaScript console (Chrome or something) with jQuery.

var jo = $("<xml required="true" name="lol"></xml>"); //test xml
jo.attr("required"); //returns "required" instead of true
jo.attr("name"); //returns correct "lol"
jo.prop("required"); //returns undefined
jo.prop("name"); //returns undefined

Anyone out there with a working solution to get the correct required value ( true/false ) of this xml?

.prop() only works with html like <input> .


The jQuery code has its own opinions on attributes and their nature, based on their names and semantics from the HTML world. That's why "required" gives you "required" as its attribute value.

You can try using .getAttribute() directly, though its results might be browser-dependent:

jo[0].getAttribute("required");

The .prop() function only works for HTML DOM elements because it relies on the browser creating objects with properties reflecting attribute values parsed from the source.

链接地址: http://www.djcxy.com/p/4740.html

上一篇: 应该用prop()还是attr()来设置href?

下一篇: XML解析所需的attr?! 支柱?