jQuery 属性选择器element[herf*='value']使用示例

一个针对jQuery属性选择器的小例子,增加对jQUery属性选择器的理解:


代码如下:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<style type="text/css">
a{
margin-right:20px;
}
ol{
position:relative;
width:600px;
margin-left:400px;
}
dt{
margin:10px;
height:100px;
background-color:#EAEAEA;
border:3px dotted orange;
}
.showMessage{
width:380px;
float:left;
background-color:#D8D8D8;
border:1px dotted red;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
var subject = "";
var describe = "";

//name|="value"
$("#attri1").bind("click",function(){
var topValue=$("#attri1").offset().top;
subject = "Attribute Contains Prefix Selector [name|=\"value\"]";
describe = "Selects elements that have the specified attribute with a value either equal to a given string or starting with that string followed by a hyphen (-).";
$("a[hreflang|='en']").css("border","3px dotted green");
showMessage(subject,describe,topValue);
});

//name*="value"
$("#attri2").bind("click",function(){
var topValue=$("#attri2").offset().top;
subject = "Attribute Contains Selector [name*=\"value\"]";
describe = "Selects elements that have the specified attribute with a value containing the a given substring.";
$( "input[name*='man']" ).val( "has man in it!" );
showMessage(subject,describe,topValue);
});

//name~="value"
$("#attri3").bind("click",function(){
var topValue=$("#attri3").offset().top;
subject = "Attribute Contains Word Selector [name~=\"value\"]";
describe = "Selects elements that have the specified attribute with a value containing a given word, delimited by spaces.";
$( "input[name~='man']" ).val( "mr. man is in it!" );
showMessage(subject,describe,topValue);
});

//name$="value"
$("#attri4").bind("click",function(){
var topValue=$("#attri4").offset().top;
subject = "Attribute Ends With Selector [name$=\"value\"]";
describe = "Selects elements that have the specified attribute with a value ending exactly with a given string. The comparison is case sensitive.";
$( "input[name$='letter']" ).val( "a letter" );
showMessage(subject,describe,topValue);
});

//name="value"
$("#attri5").bind("click",function(){
var topValue=$("#attri5").offset().top;
subject = "Attribute Equals Selector [name=\"value\"]";
describe = "Selects elements that have the specified attribute with a value exactly equal to a certain value.";
$( "input[value='Hot Fuzz']" ).next().text( "Hot Fuzz" );
showMessage(subject,describe,topValue);
});

//name$="value"
$("#attri6").bind("click",function(){
var topValue=$("#attri6").offset().top;
subject = "Attribute Not Equal Selector [name!=\"value\"]";
describe = "Select elements that either don't have the specified attribute, or do have the specified attribute but not with a certain value.";
$( "input[name!='newsletter']" ).next().append( "<b>; not newsletter</b>" );
showMessage(subject,describe,topValue);
});

//name$="value"
$("#attri7").bind("click",function(){
var topValue=$("#attri7").offset().top;
subject = "Attribute Starts With Selector [name^=\"value\"]";
describe = "Selects elements that have the specified attribute with a value beginning exactly with a given string.";
$( "input[name^='news']" ).val( "news here!" );
showMessage(subject,describe,topValue);
});

//name$="value"
$("#attri8").bind("click",function(){
var topValue=$("#attri8").offset().top;
subject = "Has Attribute Selector [name]";
describe = "Selects elements that have the specified attribute, with any value.<br><b><font color=\"red\">you can click the div which have id element</font></b>";
$( "div[id]" ).one( "click", function() {
var idString = $( this ).text() + " = " + $( this ).attr( "id" );
$( this ).text( idString );
});
showMessage(subject,describe,topValue);
});

//name$="value"
$("#attri9").bind("click",function(){
var topValue=$("#attri9").offset().top;
subject = "Multiple Attribute Selector [name=\"value\"][name2=\"value2\"]";
describe = "Matches elements that match all of the specified attribute filters.";
$( "input[id][name$='man']" ).val( "only this one" );
showMessage(subject,describe,topValue);
});

});

function showMessage(subject,describe,topValue){
$("#showMessage").html("<font color=\"red\"><b>"+subject+"</b></font><br>"+describe)
.addClass("showMessage").css("margin-top",topValue).hide().show(1000);
}

</script>
</head>
<body>
<div id="showMessage"></div>
<ol>
<dt>
<input type="button" id="attri1" value="a[hreflang|='en']"/><br><br>
<a href="#" hreflang="en">en</a>
<a href="#" hreflang="en-">en-</a>
<a href="#" hreflang="english">english</a>
</dt>
<dt>
<input type="button" id="attri2" value="name*='man'"/><br><br>
<input name="man-news">
<input name="milkman"><br>
<input name="letterman2">
<input name="newmilk">
</dt>
<dt>
<input type="button" id="attri3" value="input[name~='man']"/><br><br>
<input name="man-news">
<input name="milk man"><br>
<input name="letterman2">
<input name="newmilk">
</dt>

<dt>
<input type="button" id="attri4" value="input[name$='letter']"/><br><br>
<input name="newsletter">
<input name="milkman"><br>
<input name="jobletter">
</dt>

<dt>
<input type="button" id="attri5" value="input[value='Hot Fuzz']"/><br><br>
<div>
<label>
<input type="radio" name="newsletter" value="Hot Fuzz">
<span>name?</span>
</label>
</div>
<div>
<label>
<input type="radio" name="newsletter" value="Cold Fusion">
<span>value?</span>
</label>
</div>
<div>
<label>
<input type="radio" name="newsletter" value="Evil Plans">
<span>value?</span>
</label>
</div>
</dt>

<dt>
<input type="button" id="attri6" value="input[name!='newsletter']"/><br><br>
<div>
<input type="radio" name="newsletter" value="Hot Fuzz">
<span>name is newsletter</span>
</div>
<div>
<input type="radio" value="Cold Fusion">
<span>no name</span>
</div>
<div>
<input type="radio" name="accept" value="Evil Plans">
<span>name is accept</span>
</div>
</dt>

<dt>
<input type="button" id="attri7" value="input[name^='news']"/><br><br>
<input name="newsletter">
<input name="milkman"><br>
<input name="newsboy">
</dt>

<dt>
<input type="button" id="attri8" value="div[id]"/><br>
<div>no id</div>
<div id="hey">with id</div>
<div id="there">has an id</div>
<div>nope</div>
</dt>

<dt>
<input type="button" id="attri9" value="input[id][name$='man']"/><br><br>
<input id="man-news" name="man-news">
<input name="milkman"><br>
<input id="letterman" name="new-letterman">
<input name="newmilk">
</dt>

<dt>
<input type="button" value="clearEffects" onclick="javaScript:window.location.reload();"/>
</dt>
</ol>
</body>
</html>

(0)

相关推荐

  • jQuery Selectors(选择器)的使用(九、表单对象属性篇)

    本系列文章分为:基本篇.层次篇.简单篇.内容篇.可见性篇.属性篇.子元素篇.表单篇.表单对象属性篇共9篇文章. 本篇讲解::enabled,:disabled,:checked,:selected的用法. 您对本系列文章有任何建议或意见请发送到邮箱:sjzlgt@qq.com 由于是第一次写技术性系列文章,难免会出错或代码BUG,欢迎指出,在此谢过! 运行后,需要刷新下,加载下jquery jQuery-Selectors-9 .div { width:95%; margin-left:15px

  • JQuery中属性过滤选择器用法实例分析

    本文实例讲述了JQuery中属性过滤选择器用法.分享给大家供大家参考.具体如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <he

  • jQuery属性选择器用法示例

    本文实例讲述了jQuery属性选择器用法.分享给大家供大家参考,具体如下: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script src="js/jquery-1.10.1.min.js" type="text/

  • jquery属性过滤选择器使用示例

    复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title

  • jquery 属性选择器(匹配具有指定属性的元素)

    jQuery 选择器 在前面的章节中,我们展示了一些有关如何选取 HTML 元素的实例. 关键点是学习 jQuery 选择器是如何准确地选取您希望应用效果的元素. jQuery 元素选择器和属性选择器允许您通过标签名.属性名或内容对 HTML 元素进行选择. 选择器允许您对 HTML 元素组或单个元素进行操作. 在 HTML DOM 术语中: 选择器允许您对 DOM 元素组或单个 DOM 节点进行操作. jQuery 元素选择器 jQuery 使用 CSS 选择器来选取 HTML 元素. $("

  • jQuery Selectors(选择器)的使用(六、属性篇)

    本系列文章分为:基本篇.层次篇.简单篇.内容篇.可见性篇.属性篇.子元素篇.表单篇.表单对象属性篇共9篇文章. 本篇讲解:[attribute],[attribute=value],[attribute!=value],[attribute^=value],[attribute$=value],[attribute*=value],[selector1][selector2][selectorN]的用法. 您对本系列文章有任何建议或意见请发送到邮箱:sjzlgt@qq.com 由于是第一次写技术

  • jquery选择器-根据多个属性选择示例代码

    根据多个属性选择E[attr=val][attr=val] $("div[title='ttt'][class='aaaa']").click()................ 所有div元素下所有属性title值是等于ttt并且属性class等于aaaa的元素

  • jquery选择器、属性设置用法经验总结

    本人是一名小白,应届毕业生,以前没用过jquery,最近做项目用到了jquery.在做的过程中走了很多弯路,不停的搜索.总结出了一些用法,供大家参考: 最基本的选择器语法包括:id.class.标签.属性,这和css选择器是一致的. ID选择器要在ID前加#,比如要选择一个ID为myDivID的div元素(<div id="myDivID"></div>)可以这样写: 复制代码 代码如下: $("#myDivID"); D是不能重复的,所以I

  • jQuery中复合属性选择器用法实例

    本文实例讲述了jQuery中复合属性选择器用法.分享给大家供大家参考.具体分析如下: 此选择器能够匹配同时满足多个属性条件的元素. 语法结构: 复制代码 代码如下: [selector1][selector2][selectorN] 参数列表: 参数 描述 selector1 属性选择器. selector2 另一个属性选择器,用以进一步缩小范围. selectorN 任意多个属性选择器,也是用来缩小范围. 实例代码: 复制代码 代码如下: <!DOCTYPE html> <html&g

  • jquery表单对象属性过滤选择器实例分析

    本文实例讲述了jquery表单对象属性过滤选择器用法.分享给大家供大家参考.具体分析如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> &

  • jquery属性选择器not has怎么写 行悬停高亮显示

    复制代码 代码如下: $(function(){ /* //$("html body div table.table_list tbody tr").not(":has('td a img[src*=tj.gif]')").mouseover(function(){ $("html body div table.table_list tbody tr:not(':has('td a img[src*=tj.gif]')')").mouseover

  • jQuery子属性过滤选择器用法分析

    本文实例讲述了jQuery子属性过滤选择器用法.分享给大家供大家参考.具体分析如下: 1. :first-child选择器 用于选择其父级的第一个子元素的所有元素,格式: 复制代码 代码如下: $("selector:first-child") 如: 复制代码 代码如下: $("ul:first-child").css("text-decoration", "underline").css("color",

随机推荐