欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳談jQuery操縱DOM元素屬性 attr()和removeAtrr()方法

 更新時(shí)間:2015年01月22日 10:09:07   投稿:hebedich  
這篇文章主要詳細(xì)介紹了jQuery操縱DOM元素屬性 attr()和removeAtrr()方法,非常的全面細(xì)致,在這里推薦給小伙伴們。

  jQuery中操縱元素屬性的方法:
  attr(): 讀或者寫匹配元素的屬性值.
  removeAttr(): 從匹配的元素中移除指定的屬性.

attr()方法 讀操作

  attr()讀操作. 讀取的是匹配元素中第一個(gè)元素的指定屬性值.
  格式: .attr(attributeName),返回值類型:String.讀取不存在的屬性會(huì)返回undefined.
 
  注意選擇器的選擇結(jié)果可能是一個(gè)集合,這里僅僅獲取的是集合中第一個(gè)元素的該屬性值.
  看例子:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="/jquery/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("button").click(function () {
                alert($("p").attr("title"));//獲取屬性
                // this code can only get the first element's attribute.
            });
        });
    </script>
</head>
<body>
<p title="title1">paragraph 1</p>
<p title="title2">paragraph 2</p>
<br/>
<button>get title</button>
</body>
</html>

  運(yùn)行結(jié)果:彈框顯示: title1.
 
  想要分別獲取每一個(gè)元素的屬性,需要使用jQuery的循環(huán)結(jié)構(gòu),比如.each()或.map()方法.
  上面的例子可以改成:

復(fù)制代碼 代碼如下:

<script type="text/javascript">
    $(document).ready(function () {
        $("button").click(function () {
            //get attribute for every element in selection.
            $("p").each(function () {
                alert($(this).attr("title"));
            });
        });
    });
</script>

  即可分別獲取每個(gè)元素的屬性.

attr()方法 寫操作

  attr()寫操作. 為匹配元素的一個(gè)或多個(gè)屬性賦值.
  一般格式: .attr(attributeName, value), 即為屬性設(shè)置value.
  返回值類型:jQuery.也即支持鏈?zhǔn)椒椒ㄕ{(diào)用.
 
  執(zhí)行寫操作的時(shí)候,如果指定的屬性名不存在,將會(huì)增加一個(gè)該名字的屬性,即增加自定義屬性,其名為屬性名,其值為value值.
 
  寫屬性是為匹配集合中的每一個(gè)元素都進(jìn)行操作的,見例子:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="/jquery/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#button1").click(function(){
                $("p").attr("title","Hello World");
            });
        });
    </script>
</head>
<body>
<input type="button" id="button1" value="button1"></input>
<p>This is a paragraph.</p>
<div>This is a div.</div>
<p>This is another paragraph.</p>
<div>This is another div.</div>
</body>
</html>

 
  點(diǎn)擊按鈕之后所有的p都加上了title="Hello World”的屬性.

  寫操作還有以下兩種格式:
  .attr(attributes)和.attr(attributeName, function).
  下面分別介紹.

.attr(attributes):

  這里attributes類型是PlainObject,可以用于一次性設(shè)置多個(gè)屬性.
  什么是PlainObject呢,簡單理解就是大括號(hào)包圍的鍵值對(duì)序列.可以參考問后鏈接說明.
  鍵和值之間用冒號(hào)(:)分隔,每個(gè)鍵值對(duì)之間用逗號(hào)(,)分隔.
 
  注意: 設(shè)置多個(gè)屬性值時(shí),屬性名的引號(hào)是可選的(可以有,也可以沒有).但是class屬性是個(gè)例外,必須加上引號(hào).
 
  例子:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="/jquery/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#button1").click(function () {
                $("p").attr("title", "Hello World");
            });
            $("#button2").click(function () {
                $("div").attr({"title": "some title for div", "hello": "World"});
            });
        });
    </script>
</head>
<body>
<input type="button" id="button1" value="button1"></input>
<input type="button" id="button2" value="button2"></input>
<p>This is a paragraph.</p>
<div>This is a div.</div>
<p>This is another paragraph.</p>
<div>This is another div.</div>
</body>
</html>

  點(diǎn)擊兩個(gè)按鈕之后,元素變?yōu)?

  其中<div>的hello是一個(gè)新增的自定義屬性,其value為World.
 
.attr(attributeName, function(index, oldValue)):
  使用一個(gè)function來設(shè)置屬性值.function的第一個(gè)參數(shù)是index,第二個(gè)參數(shù)是該屬性之前的值.
  看例子:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<style>
div {
color: blue;
}
span {
color: red;
}
b {
font-weight: bolder;
}
</style>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("div")
.attr("id", function (index, oldAttr) {
if (oldAttr) {
return "div-id" + index + oldAttr;
} else {
return "div-id" + index;
}
})
.each(function () {
$("span", this).html("(id = '<b>" + this.id + "</b>')");
});
});
</script>
</head>
<body>
<div id="someId">Zero-th <span></span></div>
<div>First <span></span></div>
<div>Second <span></span></div>
</body>
</html>

  上面的例子,對(duì)應(yīng)的頁面結(jié)果如下:

 
  當(dāng)使用一個(gè)方法來設(shè)定屬性值的時(shí)候,如果這個(gè)set的function沒有返回值,或者返回了undefined,當(dāng)前的值是不會(huì)被改變的.
  即操作會(huì)被忽略.
  還是上面的例子,attr()其中的function返回undefined:
  如下:

復(fù)制代碼 代碼如下:

<script type="text/javascript">
    $(document).ready(function () {
        $("div").attr("id", function (index, oldAttr) {
            return undefined;
        }).each(function () {
            $("span", this).html("(id = '<b>" + this.id + "</b>')");
        });
    });
</script>

 
  返回的頁面效果如下:

  即沒有進(jìn)行任何修改操作,還是保持原來的屬性值.
 
  注意:jQuery不能修改<input>和<button>的type屬性,如果修改會(huì)在瀏覽器報(bào)錯(cuò).
  這是因?yàn)镮E瀏覽器中不能修改type屬性.

removeAttr()方法

  移除匹配元素集合中每一個(gè)元素的指定屬性.
  removeAttr()方法調(diào)用的是JavaScript的removeAttribute()方法,但是它能用jQuery對(duì)象直接調(diào)用,并且它考慮到并處理了各個(gè)瀏覽器上的屬性名稱可能不統(tǒng)一的問題.
  例子:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="/jquery/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("input[type=button]").click(function () {
                $("div").removeAttr("title");
            });
        });
    </script>
</head>
<body>
<input type="button" value="ClickMe"></input>
<div title="hello">Zero</div>
</body>
</html>

 
  點(diǎn)擊按鈕后,<div>的title屬性會(huì)被刪除.
 
  注意: 用removeAttr()移除onclick在IE6-8上都不會(huì)起作用,為了避免這個(gè)問題,應(yīng)該使用.prop()方法.
  比如:
復(fù)制代碼 代碼如下:

$element.prop( "onclick", null );
console.log( "onclick property: ", $element[ 0 ].onclick );

相關(guān)文章

最新評(píng)論