三個簡單實用的用於DOM操作jQuery方法,我個人老是容易記混:
test():設置或返回所選元素的文本內容
html():設置或返回所選元素的內容(包括HTML標記)
val():設置或返回表單字符段的值(input標簽中的值)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="jquery/jquery-3.3.1.min.js"></script>
<script>
$(
function(){
$("#btn0").click(
function(){
alert($("#div1").text());
alert($("#div2").text());
alert($("#div1").html());
alert($("#div2").html());
alert($("input").val());
}
)
}
)
$(
function(){
$("#btn1").click(
function(){
$("#div1").text("<b>初終</b>");
$("#div2").text("初心");
}
)
}
)
$(
function(){
$("#btn2").click(
function(){
$("#div1").html("<b>初終</b>");
$("#div2").html("初心");
}
)
}
)
$(
function(){
$("#btn3").click(
function(){
$("input").val("左右");
}
)
}
)
</script>
</head>
<body>
<div id="div1" style=" border:1px solid gray ;width: 150px; height: 30px">
從未
</div>
<hr>
<div id="div2" style="border:1px solid gray ;width: 150px; height: 30px">
左右
</div>
<hr>
<input type="text" value="昔日">
<hr>
<div>
<button type="button" id="btn0">獲取內容</button>
<button type="button" id="btn1">改變text內容</button>
<button type="button" id="btn2">改變html內容</button>
<button type="button" id="btn3">改變value內容</button>
</div>
</body>
</html>
---------------------
作者:一心不安
來源:CSDN
原文:https://blog.csdn.net/qq_42700595/article/details/84541112
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
