js中的innerText、innerHTML、属性值、value与jQuery中的text()、html()、属性值、val()总结...

js与jQuery获取text、html、属性值、value的方法是不一样的。

 <p id="test">这是段落中的 <b>粗体</b> 文本。</p> 
有上面这样一个文本框

js获取text、html、属性值、value的方法:
document.getElementById("test").innerText;
document.getElementById("test").innerHTML;
document.getElementById("test").id;
document.getElementById("test1").value;

jQuery获取text、html、属性值、value的方法:
$("#test").text()或者$("#test").innerText
$("#test").html()或者$("#test").innerHTML
$("#test").attr("id")
$("#test").attr("value")或者$("#test1").val()或者$("#test1").value
 
 
 

js与jQuery,text与innerText获取(<!---->中为结果)

html:
    <p id="test">这是段落中的 <b>粗体</b> 文本。</p>
    <button id="btn10">jQuery显示text</button><!--结果   Text: 这是段落中的 粗体 文本。-->
    <button id="btn11">jQuery显示 innerTEXT</button><!--结果   Text: undefined。-->
    <button id="btn12">js显示 innerTEXT</button><!--结果   Text: test-->
js:
    $("#btn10").click(function(){
    alert("Text: " + $("#test").text());
  });
    $("#btn11").click(function(){
    alert("Text: " + $("#test").innerText);
  });
    $("#btn12").click(function(){
    alert("Text: " + document.getElementById("test").innerTEXT
); });

 

js与jQuery,html与innerHTML获取

html:
<p id="test">这是段落中的 <b>粗体</b> 文本。</p>
    <button id="btn20">jQuery显示 HTML</button><!--结果   HTML: 这是段落中的 <b>粗体</b> 文本。 -->
    <button id="btn21">jQuery显示 innerHTML</button><!--结果   HTML: undefined -->
    <button id="btn22">js显示 innerHTML</button><!--结果  HTML: 这是段落中的 <b>粗体</b> 文本。 -->
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:
<button id="btn30">js获取Id的属性值</button><!--结果 id: test --> <button id="btn31">jQuery获取Id的属性值</button><!--结果 id: test -->

js:
     $("#btn30").click(function(){
    alert("id: " + document.getElementById("test").id);
  });
    $("#btn31").click(function(){
    alert("id: " + $("#test").attr("id"));
  });

 

js与jQuery,value获取

html:
<button id="btn40">js获取input标签的value值</button><!--结果   value: aaa -->
    <button id="btn41">jQuery获取input标签的value值(val())</button><!--结果   value: aaa -->
    <button id="btn42">jQuery获取input标签的value值(attr("value"))</button><!--结果  value: aaa -->
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值获取

转载于:https://www.cnblogs.com/mlbblkss/p/7135871.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值