JQuery選取器之[attribute^=value]使用注意事項,jqueryattribute

來源:互聯網
上載者:User

JQuery選取器之[attribute^=value]使用注意事項,jqueryattribute

注意事項一:

Firefox下JQuery選取器之[attribute^=value]使用注意事項

之前寫的一個指令碼中用到了

var bindAttrs = $("[databind^='attr'", item);
大家都看出存在的問題了吧?

這指令碼用了一段時間了,但一直沒用Firefox下測試過,

在其它瀏覽器下都能正常使用!


今天突然有人發現在Firefox下載入的資料不正確,

經過一段時間的調試,最後發現是代碼寫的有問題,

犯了一個低級的錯誤,唯寫了左中括弧,忘了寫右中括弧。

注意事項二:

相關HTML:

<div id="Zy_hotsort_img" class="Zy_hotsort_img"><img src="${ctx}/images/resource/jpg02.jpg" databind="attr:{src:=http://{{newCourseware.coverpath}}}" ><div class="Zy_newicon">new</div></div>
相關JS代碼:

var item = $("#Zy_hotsort_img").html();var bindAttrs = $("*[databind^='attr']", item);
問題出現了,這樣子是擷取不到databind屬性為attr開頭的img元素的,那應該怎麼寫呢?

寫法一:

var item = $("#Zy_hotsort_img").prop("outerHTML");var bindAttrs = $("*[databind^='attr']", item);

寫法二:

var item = $("#Zy_hotsort_img").html();var bindAttrs = $("*[databind^='attr']", $("<div></div>").append(item));


jquery [attribute=value] 變數

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/...al.dtd">
<html xmlns="www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<style>
.on{color:#FF0000;}
</style>
<script src="js/jquery.js"></script>
<script>
$(function() {
$("a").click(function(){
var test1 = $(this).attr("class")+"-img"
$("p[class=" + test1 + "]").addClass("on")
});
});
</script>
</head>

<body>
<a href="#" class="a1">我是連結一,class="a1"</a>    
<a href="#" class="a2">我是連結二,class="a2"</a><br/>

<p class="a1-img">我是p1,class="a1-img"</p>
<p class="a2-img">我是p2,class="a2-img"</p>
</body>
</html>

---------------------

這個就可以啦。...餘下全文>>
 
jquery的選取器中可以使用使用Regex?

$("[class^='class_']")
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.