<p>1234</p>
<img src="../images/logo.jpg" alt="welcome" />
<script type="text/javascript">
$(function(){
/*-----------jquery使用attr()方法设置属性-----------------*/
$("p").attr("title","this is title");
//jquery的attr()方法可以批量设置属性
$("img").attr({"src":"images/banner.jpg", "title":"this is image"});
//如果只有一个参数的话就是获取
var $_title = $("img").attr("title");
alert($_title);
//jquery删除属性用removeAttr()方法
$("p").removeAttr("title");
});
window.οnlοad= function(){
/*-----------------js使用setAttribute()方法设置属性----------------*/
//js的setAttribute()方法不能批量设置属性
var p = document.getElementsByTagName("p")[0];
p.setAttribute("title","this is js title");
//js获取属性的方法是getAttribute()
p.getAttribute("title");
//js删除属性的方法是removeAttribute()
document.getElementsByTagName("p")[0].removeAttribute("title");
}
</script>
jquery和js操作属性
最新推荐文章于 2023-10-13 18:00:25 发布