jQuery获取元素



**********************获取元素****************************
 //返回id为msg的元素节点的html内容。
$(”#msg”).html();   

//将“<b>new content</b>”作为html串写入id为msg的元素节点内容中,页面显示粗体的new

content(会解析)
$(”#msg”).html(”<b>new content</b>”);

//返回id为msg的元素节点的文本内容。
$(”#msg”).text();    

//将“<b>new content</b>” 作为普通文本串写入id为msg的元素节点内容中,页面显示<b>new

content</b>(不会解析)
$(”#msg”).text(”<b>new content</b>”);

//返回表单输入框的value值
$(”input”).val(”);

//将表单输入框的value值设为test
$(”input”).val(”test”);


设置text为pxx的项选中
$(".selector").find("option[text='pxx']").attr("selected",true);

获取当前选中项的text
$(".selector").find("option:selected").text();

//选择被选中Radio的Value值
$("input[name='radio_name'][checked]").val();  

//根据Value值设置Radio为选中状态
$("input[name='radio_name'][value='要选中Radio的Value值'").attr("checked",true);  

//选择被选中CheckBox元素的集合 如果你想得到Value值你需要遍历这个集合
$("input[name='checkbox_name'][checked]");

//找到所有含有 id 属性,并且它的 name 属性是以 man 结尾的
$("input[id][name$='man']")

//查找所有 name 包含 'man' 的 input 元素
$("input[name*='man']")

//查找所有 name 以 'letter' 结尾的 input 元素
$("input[name$='letter']")

//查找所有 name 以 'news' 开始的 input 元素
$("input[name^='news']")

//查找所有 name 属性不是 newsletter 的 input 元素
$("input[name!='newsletter']").attr("checked", true);

//查找所有 name 属性是 newsletter 的 input 元素
$("input[name='newsletter']").attr("checked", true);

//查找作为父元素的span类型子元素中的"长子"的span标签
$("span:first-of-type");

//查找隐藏、可见的 tr
$("tr:hidden") $("tr:visible")

//匹配type为hidden的元素
$("input:hidden")

//把src属性的值设置为title属性的值。
$("img").attr("title", function() { return this.src });

$('a').filter('.external') 获取类为external的<a>元素

原文地址:https://www.cnblogs.com/Hsong/p/5189238.html