获取匹配元素集中第一个元素的属性值,或为每个匹配元素设置一个或多个属性(会检测元素是否有指定属性)
xiyueta().prop()属性方法,三种调用方式:
1、xiyueta("div").prop("id"); //获取匹配元素集中第一个元素的属性值
2、xiyueta("div").prop("id","setvalue"); //为单个匹配元素设置属性
3、xiyueta("div").prop({id:"nav",name:"daohang"}); //为每个匹配元素以对象方式设置属性
总结:xiyueta().prop("name","") 第二个参数如果为空的话则为删除等于==xiyueta().removeAttr(),与jQuery里稍微不同,jQuery里会留属性名称,如 <a href class>,而xiyueta里直接删除掉,因为这个属性没有存在的意义了。jQuery是直接操作网页,而xiyueta操作html字符串,没有在网页里展示效果的需要,所以可以将这个元素的属性名去除掉,因为这样处理之后的html比较整洁干净。
xiyueta().prop()用法与jQuery中jQuery().prop()用法保持一致。
xiyueta().prop(function(i){})可以向jQuery里一样来使用
see https://api.jquery.com/prop
尝试一下>>
<script src="https://www.xiyueta.com/js/xiyueta.min.js"></script> <script>
var html='<div></div><a></a><img>'; $.load(html); //解析html $("*").prop("href","1.html").prop({"class":"test",'src':"1.jpg"}) $.log( $.html("format") );
</script>
<div class="test"></div>
<a href="1.html" class="test"></a>
<img class="test" src="1.jpg">