jQuery HTML 操作 改變/添加文本值


1、JQuery 改變p中的文本值

<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
  $("p").html("Update text");
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button type="button">Click here</button>
</body>

</html>

 2、JQuery 添加p中的文本值

<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
  $("p").append(" Append text");
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button type="button">請點擊這里</button>
</body>

</html>

 3、after() 函數在所有匹配的元素之后插入 HTML 內容

<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script> 

<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
  $("p").after(" Add  content.");
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button type="button">Click here</button>
</body>

</html>

 4、jQuery HTML 操作

函數 描述
$(selector).html(content) 改變被選元素的(內部)HTML
$(selector).append(content) 向被選元素的(內部)HTML 追加內容
$(selector).prepend(content) 向被選元素的(內部)HTML “預置”(Prepend)內容
$(selector).after(content) 在被選元素之后添加 HTML
$(selector).before(content) 在被選元素之前添加 HTML


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM