JQuery 四个常用的DOM操作获得内容 - text()、html()、value 以及 val() ;取属性值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(){
  $("#bnt1").click(function(){
    alert($("#baidu").attr("action"));//取id=baidu 中action属性的值
  });
  $("#bnt2").click(function(){
   alert($("#xs").text())//取id=xs 文本值 
  })
$("#bnt3").click(function(){
   alert($("#xs").html())//取id=xs的html 
  })
    $("#bnt4").click(function(){
   alert($("#bnt4").val())//取id=xs的value值 
  })
});
</script>
</head>

<body>
<p><a href="http://www.runoob.com" id="runoob">菜鸟教程</a></p>
    <p id ="xs"><a href="http://www.baidu.com" id="baidu", action="_back"><b>百度</b>一下</a></p>
<button id="bnt1">显示 href 属性的值</button>
<button id="bnt2">显示  href 的文本值</button>
<button id="bnt3">显示  href 的HTML</button>
<button id="bnt4" value="888">显示  href 的HTML</button>
</body>
</html>

设置值:

//$("p").text("给段赋新值")

//$("p").html("给段赋新值")

//$("p").value("给段赋新值")

text \ html回调函数

$("#btn1").click(function(){ $("#test1").text(function(i,origText)

{ return "旧文本: " + origText + " 新文本: Hello world! (index: " + i + ")"; }); });

$("#btn2").click(function(){

$("#test2").html(function(i,origText)

{ return "旧 html: " + origText + " 新 html: Hello <b>world!</b> (index: " + i + ")"; }); });

猜你喜欢

转载自blog.csdn.net/DN_XIAOXIAO/article/details/83627547