JQeury修改标签属性

在<img class="cycle" id="cycle1" src="" width=15 height=15>标签中,如何根据具体情况,添加或者删除src呢?

JQuery中有一个attr的属性,attr() 方法设置或返回被选元素的属性值。

语法:$(selector).attr(attribute,function(index,oldvalue))

因此,只需要对选定的容器,采用attr函数,就可以修改里面的属性。

例如:$(".cycle").attr("src","./img/circle.jpg");

这样,我们就对类名为cycle的img标签中的src添加值,也就是img添加了一张名为circle的图片。

接下来,如果要移除这张图片,就要利用removeAttr()函数,即:

$(".cycle").removeAttr("src");

removeAttr()表示从某个元素中移除某个属性。

 

posted @ 2015-03-03 11:09  hershin  阅读(288)  评论(0编辑  收藏  举报