jQuery get content

 address

  • text () - Set or returns the selected text element
  • html () - Set or returns the contents of the selected elements (HTML tags )
  • val () - Set or returns form field value of

A, text () html ()

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#btn1").click(function(){
    alert("Text: " + $("#test").text());
  });
  $("#btn2" ) .Click ( function () { 
    Alert ( " the HTML: "  + $ ( " #test " ) .html () ); 
  }); 
}); 
</ Script > 
</ head > 

< body > 
< P ID = "Test" > this is a paragraph < B > bold </ B > text. </ P > 
< Button ID = "btn1" > text </button>
<button id="btn2">显示 HTML</button>
</body>
</html>

 

 Two, val ()

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    alert("值为: " + $("#test").val());
  });
});
</script> 
</ Head > 

< body > 
< P > Name: < INPUT type = "text" ID = "Test" value = "novice Tutorial" > </ P > 
< Button > display value </ Button > 
</ body > 
< / HTML >

 

 

Val () can be obtained text entry field <textarea> input content :

 







 
</script>
</head>

<body>
<textarea id="test"></textarea>
<button>显示值</button>
</body>
</html>

 

 

 Third, access to property attr ()

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    alert($("#runoob").attr("href"));
  });
});
</script>
</head>

<body>
<p><a href="//www.runoob.com" id="runoob">菜鸟教程</a></p>
<button>显示 href 属性的值</button>
</body>
</html>

 

 

 

 

prop()函数的结果:

      1.如果有相应的属性,返回指定属性值。

      2.如果没有相应的属性,返回值是空字符串。

attr()函数的结果:

      1.如果有相应的属性,返回指定属性值。

      2.如果没有相应的属性,返回值是 undefined。

对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。

对于HTML元素我们自己自定义的DOM属性,在处理时,使用 attr 方法。

具有 true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop()

 

Guess you like

Origin www.cnblogs.com/expedition/p/11406916.html