惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
C
Cybersecurity and Infrastructure Security Agency CISA
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tailwind CSS Blog
G
GRAHAM CLULEY
博客园 - 叶小钗
T
Threatpost
小众软件
小众软件
The Hacker News
The Hacker News
博客园 - 聂微东
博客园 - 三生石上(FineUI控件)
P
Privacy & Cybersecurity Law Blog
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
Jina AI
Jina AI
S
Schneier on Security
N
News | PayPal Newsroom
Help Net Security
Help Net Security
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
T
Troy Hunt's Blog
美团技术团队
L
Lohrmann on Cybersecurity
The Last Watchdog
The Last Watchdog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
B
Blog RSS Feed
The Register - Security
The Register - Security
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
S
SegmentFault 最新的问题
腾讯CDC
云风的 BLOG
云风的 BLOG
Simon Willison's Weblog
Simon Willison's Weblog
Google DeepMind News
Google DeepMind News
AI
AI
GbyAI
GbyAI
Attack and Defense Labs
Attack and Defense Labs
Cloudbric
Cloudbric
I
Intezer
The GitHub Blog
The GitHub Blog
V2EX - 技术
V2EX - 技术
Scott Helme
Scott Helme
J
Java Code Geeks

博客园 - 礼拜一

MAC实用技巧 。 工业设计的10个基本法则 Web可用性设计的247条指导方针 选中文本,变绿~ Examples of location paths using the unabbreviated syntax C# Console:利用mspaint打开图像并保存 。 15戒律 HttpWebRequest默认的只有2个并发连接限制!!!!! 获取远程XML文档 在外企工作,一些英語用法潜规则 vs2008命令窗口使用 Visual Studio 2005 中的命令窗口 Error: No such interface supported 动态加载UserControl~ - 礼拜一 - 博客园 英语常用高频语句 XSLT模式匹配的语法 XPath表达式(缩写和详写方式) 穷死了,看电子书吧~ C#里边的控件缩写大全(比较规范)
jQuery的一些备忘 - 礼拜一 - 博客园
礼拜一 · 2009-11-06 · via 博客园 - 礼拜一

操作元素的样式

主要包括以下几种方式:

$("#msg").css("background"); //返回元素的背景颜色

$("#msg").css("background","#ccc") //设定元素背景为灰色

$("#msg").height(300); $("#msg").width("200"); //设定宽高

$("#msg").css({ color: "red", background: "blue" }); //以名值对的形式设定样式

$("#msg").addClass("select"); //为元素增加名称为select的class

$("#msg").removeClass("select"); //删除元素名称为select的class

$("#msg").toggleClass("select"); //如果存在(不存在)就删除(添加)名称为select的class

显示/隐藏元素

jQuery("#TagName").hide();

jQuery("#TagName").show();

jQuery(".TagName").css("display","none");

jQuery(".TagName").css("display","");

取得第一个匹配元素的属性值(如果元素没有相应属性,则返回 undefined )

jQuery("#tagName").attr("class") 或 jQuery("#tagname").attr("className");

jQuery("#checkName").attr("checked",true); // 选中checkBox

jQuery("SELECT#TagName").attr("disabled", "disabled"); // 将某个元素设置失效

val()与text()的区别

text()方法是取得所有匹配元素的内容。结果是由所有匹配元素包含的文本内容组合起来的文本。这个方法对HTML和XML文档都有效。

获取span,div ,p之类才用text()或html()方法。

例如:

<div>wahaha</div>

jQuery("div").text(); // 将得到文本"wahaha"

单行文本<input type="text" ...>不能用text()方法获得值,必须用val()方法。

val()方法是获得第一个匹配元素的当前值。

例:

<input type="text" value="a" />

<input type="text" value=" b"/>

<input type ="text" value= "cc"/>

<select>

<option>aaa</option>

<option selected="selected">bbb</option>

<option>ccc</option>

</select>

jQuery("input").val();   

jQuery("select").val();

将得到:第一个匹配元素的值“wahaha" 和"bbb"

获取一组checbox/radio被选中项的值

<input name="ckTagName" type="checkbox" checked="true" value="nn"/>

jQuery("input[@name=ckTagName][@checked]").val()

获取一组checkbox被选中项的个数

jQuery(":checkbox[name='checkItems']:checked").length;

或通过class获取

jQuery(".pItem:checkbox:checked").length;

设置radio单选组的第二个选项为当前选中值

jQuery("input[@name=items]").get(1).checked = true;

获取select被选中项的文本

jQuery("select[@name=selcTagName] option[@selected]").text();

获取Select被选中项的Value值

jQuery("select[@name=selcTagName] option[@selected]").val();

设置Select下拉框选项的第二个元素为默认值

jQuery("#selectItems")[0].selectedIndex = 1;

索引 eq()、get()、Index()

eq(pos)

说明:减少匹配对象 到一个单独得dom元素

参数:pos (Number): 期望限制的索引,从0 开始

例子:

未执行jQuery前:

<p>This is just a test.</p>

<p>So is this</p>

<a href="#" id="test" onClick="jq()">

jQuery</a>jQuery代码及功能:

function jq(){

   alert($("p").eq(1).html())

}

运行:当点击id为test的元素时,alert对话框显示:So is this,即第二个<p>标签的内容

get() get(num)

说明:获取匹配元素,get(num)返回匹配元素中的某一个元素

参数:get (Number): 期望限制的索引,从0 开始

例子:

未执行jQuery前:

<p>This is just a test.</p>

<p>So is this</p>

<a href="#" id="test" onClick="jq()">jQuery</a>

jQuery代码及功能:

function jq(){

   alert($("p").get(1).innerHTML);

}

运行:当点击id为test的元素时,alert对话框显示:So is this,即第二个<p>标签的内容

注意get和eq的区别,eq返回的是jQuery对象,get返回的是所匹配的dom对象,所有取$("p").eq(1)对象的内容用jQuery方法html(),而取$("p").get(1)的内容用innerHTML

index(obj)

说明:返回对象索引

参数:obj (Object): 要查找的对象

例子:

未执行jQuery前:

<div id="test1"></div>

<div id="test2"></div>

<a href="#" id="test" onClick="jq()">jQuery</a>

jQuery代码及功能:

function jq(){

   alert($("div").index(document.getElementById('test1')));

   alert($("div").index(document.getElementById('test2')));

}

运行:当点击id为test的元素时,两次弹出alert对话框分别显示0,1

size() Length

说明:当前匹配对象的数量,两者等价

例子:

未执行jQuery前:

<img src="test1.jpg"/>

<img src="test2.jpg"/>

<a href="#" id="test" onClick="jq()">jQuery</a>

jQuery代码及功能:

function jq(){

   alert($("img").length);

}

运行:当点击id为test的元素时,弹出alert对话框显示2,表示找到两个匹配对象

JQuery选择器

(1)层级:

选择一[空格]选择二 表示选一内合符条件二的所有元素

选择一[>]选择二 表示选一内合符条件二的第一个元素

选择一[+]选择二 表示紧接选一符条件二的元素 next

选择一[~]选择二 表示选一后符条件二的所有元素 siblings

(2)运算符
:animated           动画元素
:eq(index)          索引位置,如:$("div:eq(1)"
:even               偶数元素
dd                奇数元素
:first              第一个
:gt(index)          大于索引的所有元素
:lt(index)          小于索引的所有元素
:header             H1、H2... 这些标题元素
:last               最后一个
:not(Selector)      排除

:contains(string)   选择的对象内容里包含
:empty              选择的对象内容为空
:has(Selector)      含有
:parent             与empty相反
:first-child
:last-child
:nth-child(index)   第几个
nly-child         唯一的子元素

表单
:text :checkbox :radio :image :file :submit :reset :password :button

表单状态
:checked :disabled :enabled :selected

可见性
:hidden :visible

属性及其运算符
[属性名称]        匹配包含给定属性的元素
[att=value]       等同上面
[att*=value]      模糊匹配
[att!=value]      不能是这个值
[att$=value]      结尾是这个值
[att^=value]      开头是这个值
[att1][att2][att3]...   匹配多个属性条件中的一个