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


Posted in Javascript onOctober 20, 2013

一个针对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>
Javascript 相关文章推荐
JavaScript 事件查询综合
Jul 13 Javascript
网站页面自动跳转实现方法PHP、JSP(上)
Aug 01 Javascript
jQuery实战之品牌展示列表效果
Apr 10 Javascript
鼠标选择动态改变网页背景颜色的JS代码
Dec 10 Javascript
JS中Eval解析JSON字符串的一个小问题
Feb 21 Javascript
JavaScript的ExtJS框架中数面板TreePanel的使用实例解析
May 21 Javascript
利用Angularjs和原生JS分别实现动态效果的输入框
Sep 01 Javascript
Vue数据驱动模拟实现2
Jan 11 Javascript
js 作用域和变量详解
Feb 16 Javascript
解决vue 按钮多次点击重复提交数据问题
May 10 Javascript
layui自定义ajax左侧三级菜单
Jul 26 Javascript
扫微信小程序码实现网站登陆实现解析
Aug 20 Javascript
js数组的基本用法及数组根据下标(数值或字符)移除元素
Oct 20 #Javascript
浏览器的JavaScript引擎的识别方法
Oct 20 #Javascript
JS实现点击图片在当前页面放大并可关闭的漂亮效果
Oct 18 #Javascript
jquery 循环显示div的示例代码
Oct 18 #Javascript
使用CSS和jQuery模拟select并附提交后取得数据的代码
Oct 18 #Javascript
简单实用的全选反选按钮例子
Oct 18 #Javascript
关于jquery的多个选择器的使用示例
Oct 18 #Javascript
You might like
PHP数组实例总结与说明
2011/08/23 PHP
php判断正常访问和外部访问的示例
2014/02/10 PHP
PHP中读取文件的几个方法总结(推荐)
2016/06/03 PHP
DEDE实现转跳属性文档在模板上调用出转跳地址
2016/11/04 PHP
PHP给源代码加密的几种方法汇总(推荐)
2018/02/06 PHP
PHP反射实际应用示例
2019/04/03 PHP
JavaScript与Div对层定位和移动获得坐标的实现代码
2010/09/08 Javascript
ExtJS4中的requires使用方法示例介绍
2013/12/03 Javascript
分享20款美化网站的 jQuery Lightbox 灯箱插件
2014/10/10 Javascript
node.js中的fs.lchmodSync方法使用说明
2014/12/16 Javascript
JavaScript中document对象使用详解
2015/01/06 Javascript
Bootstrap 模态框(Modal)插件代码解析
2016/12/21 Javascript
用jQuery实现可输入多选下拉组合框实例代码
2017/01/18 Javascript
jstree单选功能的实现方法
2017/06/07 Javascript
webstorm中vue语法的支持详解
2018/05/09 Javascript
element-ui 限制日期选择的方法(datepicker)
2018/05/16 Javascript
vue+element创建动态的form表单及动态生成表格的行和列
2019/05/20 Javascript
js实现转动骰子模型
2019/10/24 Javascript
[48:48]2014 DOTA2国际邀请赛中国区预选赛 SPD-GAMING VS Dream TIME
2014/05/21 DOTA
[15:46]教你分分钟做大人——沙王
2015/03/11 DOTA
python批量添加zabbix Screens的两个脚本分享
2017/01/16 Python
利用标准库fractions模块让Python支持分数类型的方法详解
2017/08/11 Python
Python数字图像处理之霍夫线变换实现详解
2018/01/12 Python
Python实现的tcp端口检测操作示例
2018/07/24 Python
解决django xadmin主题不显示和只显示bootstrap2的问题
2020/03/30 Python
Python抓包并解析json爬虫的完整实例代码
2020/11/03 Python
HTML5到底会有什么发展?HTML5的前景展望
2015/07/07 HTML / CSS
生产副总岗位职责
2013/11/28 职场文书
送货司机岗位职责
2013/12/11 职场文书
就业意向书范文
2014/04/01 职场文书
新年爱情寄语
2014/04/08 职场文书
植树节活动总结
2014/04/30 职场文书
2014年实习班主任工作总结
2014/11/08 职场文书
幼儿园保育员随笔
2015/08/14 职场文书
写作技巧:优秀文案必备的3种结构
2019/08/19 职场文书
nginx反向代理配置去除前缀案例教程
2021/07/26 Servers