当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery 属性选择器element[herf*=value]使用示例

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

2019年01月21日  | 移动技术网IT编程  | 我要评论

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

. 代码如下:


<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://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 p which have id element</font></b>";
$( "p[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>
<p id="showmessage"></p>
<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>
<p>
<label>
<input type="radio" name="newsletter" value="hot fuzz">
<span>name?</span>
</label>
</p>
<p>
<label>
<input type="radio" name="newsletter" value="cold fusion">
<span>value?</span>
</label>
</p>
<p>
<label>
<input type="radio" name="newsletter" value="evil plans">
<span>value?</span>
</label>
</p>
</dt>

<dt>
<input type="button" id="attri6" value="input[name!='newsletter']"/><br><br>
<p>
<input type="radio" name="newsletter" value="hot fuzz">
<span>name is newsletter</span>
</p>
<p>
<input type="radio" value="cold fusion">
<span>no name</span>
</p>
<p>
<input type="radio" name="accept" value="evil plans">
<span>name is accept</span>
</p>
</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="p[id]"/><br>
<p>no id</p>
<p id="hey">with id</p>
<p id="there">has an id</p>
<p>nope</p>
</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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网