首页 > 语言 > JavaScript > 正文

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

2024-05-06 14:34:57
字体:
来源:转载
供稿:网友
一个针对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/"]";
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选