jQuery文本值相關操作
阿新 • • 發佈:2018-11-01
val height 取值 技術 src html標簽 round 解析html title
html:給div設置值和獲取值,會解析html標簽
text:給div設置值和獲取值,不會解析html標簽
val:獲取input裏的值
代碼如下
<html> <head> <title></title> </head> <style> div{ width:100px; height:100px; background:red; margin-bottom:40px; }</style> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(function(){ $(‘.cHtml‘).html(‘<p>我是學生</p>‘) //會把p標簽給解析出來,然後我是學生內容添加到div上 console.log($(‘.cHtml‘).html()) //$(‘.cHtml‘).html()取出div裏的內容
$(‘.cText‘).text(‘<p>我是學生</p>‘) //不會解析標簽,會把標簽直接添加到div上
console.log($(‘.cText‘).text()) //$(‘.cText‘).text()取出div裏的內容
$(‘input‘).val(‘124‘) //給input框裏設置值
console.log($(‘input‘).val()) }) //取出input框的值
</script>
<body>
<div class="cHtml"></div>
<div class="cText"></div>
<input />
</body>
</html>
效果圖
jQuery文本值相關操作