js与jQuery获取text、html、属性值、value的方法是不一样的。
js与jQuery,text与innerText获取(中为结果)
html:这是段落中的 粗体 文本。
jQuery显示text
jQuery显示 innerTEXT
js显示 innerTEXT
js:
$("#btn10").click(function(){
alert("Text: " + $("#test").text());
});
$("#btn11").click(function(){
alert("Text: " + $("#test").innerText);
});
$("#btn12").click(function(){
alert("Text: " + document.getElementById("test").id);
});
js与jQuery,html与innerHTML获取
html:这是段落中的 粗体 文本。
jQuery显示 HTML
jQuery显示 innerHTML
js显示 innerHTML
js:
$("#btn20").click(function(){
alert("HTML: " + $("#test").html());
});
$("#btn21").click(function(){
alert("HTML: " + $("#test").innerHTML);
});
$("#btn22").click(function(){
alert("HTML: " + document.getElementById("test").innerHTML);
});
js与jQuery,属性值获取
html:js获取Id的属性值
jQuery获取Id的属性值
js:
$("#btn30").click(function(){
alert("id: " + document.getElementById("test").id);
});
$("#btn31").click(function(){
alert("id: " + $("#test").attr("id"));
});
js与jQuery,value获取
html:js获取input标签的value值
jQuery获取input标签的value值(val())
jQuery获取input标签的value值(attr("value"))
js:
$("#btn40").click(function(){
alert("value: " + document.getElementById("test1").value);
});
$("#btn41").click(function(){
alert("value: " + $("#test1").val());
});
$("#btn42").click(function(){
alert("value: " + $("#test1").attr("value"));
});
注意:jQuery中的val()方法只能用于input元素的value值获取