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 相关文章推荐
Ucren Virtual Desktop V2.0
Nov 07 Javascript
浅谈javascript 面向对象编程
Oct 28 Javascript
JavaScript CSS修改学习第六章 拖拽
Feb 19 Javascript
javascript中的prototype属性使用说明(函数功能扩展)
Aug 16 Javascript
JavaScript截取字符串的2个函数介绍
Aug 27 Javascript
jQuery查找节点并获取节点属性的方法
Sep 09 Javascript
AngularJS 单元测试(一)详解
Sep 21 Javascript
JS文件上传神器bootstrap fileinput详解
Jan 28 Javascript
Node.js中process模块常用的属性和方法
Dec 13 Javascript
js+css3实现旋转效果
Jan 20 Javascript
layui的select联动实现代码
Sep 28 Javascript
Vue两种组件类型:递归组件和动态组件的用法
Aug 06 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
一个简单的MySQL数据浏览器
2006/10/09 PHP
php 调用远程url的六种方法小结
2009/11/02 PHP
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发
2012/01/16 PHP
PHP连接MYSQL数据库实例代码
2016/01/20 PHP
拖动一个HTML元素
2006/12/22 Javascript
Jquery中getJSON在asp.net中的使用说明
2011/03/10 Javascript
JavaScript基础知识之数据类型
2012/08/06 Javascript
JS在textarea光标处插入文本的小例子
2013/03/22 Javascript
无缝滚动js代码通俗易懂(自写)
2013/06/19 Javascript
JS常用表单验证方法总结
2014/05/22 Javascript
jQuery实现表单提交时判断的方法
2014/12/13 Javascript
javascript实现网页子页面遍历回调的方法(涉及 window.frames、递归函数、函数上下文)
2015/07/27 Javascript
JQuery标签页效果的两个实例讲解(4)
2015/09/17 Javascript
Ajax分页插件Pagination从前台jQuery到后端java总结
2016/07/22 Javascript
node.js报错:Cannot find module 'ejs'的解决办法
2016/12/14 Javascript
Bootstrap select实现下拉框多选效果
2016/12/23 Javascript
Node.js的Mongodb使用实例
2016/12/30 Javascript
细说webpack源码之compile流程-rules参数处理技巧(1)
2017/12/26 Javascript
浅谈webpack打包之后的文件过大的解决方法
2018/03/07 Javascript
Vue实现商品详情页的评价列表功能
2019/09/04 Javascript
bootstrap实现嵌套模态框的实例代码
2020/01/10 Javascript
在Python中测试访问同一数据的竞争条件的方法
2015/04/23 Python
Python爬取网页中的图片(搜狗图片)详解
2017/03/23 Python
python 实现对数据集的归一化的方法(0-1之间)
2018/07/17 Python
python3的url编码和解码,自定义gbk、utf-8的例子
2019/08/22 Python
python pygame实现滚动横版射击游戏城市之战
2019/11/25 Python
Python3.9最新版下载与安装图文教程详解(Windows系统为例)
2020/11/28 Python
机关领导查摆四风思想汇报
2014/09/13 职场文书
个人贷款授权委托书样本
2014/10/07 职场文书
商家认证委托书格式
2014/10/16 职场文书
2015年学校团委工作总结
2015/05/26 职场文书
蜗居观后感
2015/06/11 职场文书
关于职业道德的心得体会
2016/01/18 职场文书
2019年房屋委托租赁合同范本(通用版)!
2019/07/17 职场文书
六年级作文之预言作文
2019/10/25 职场文书
Python多线程 Queue 模块常见用法
2021/07/04 Python