日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

013_替换元素

發布時間:2025/4/17 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 013_替换元素 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. replaceWith()方法

1.1. replaceWith(content)方法用指定的html內容或元素(元素本身和子元素)替換每一個匹配元素

1.2. 語法

$(selector).replaceWith(content)

1.3. 參數

1.4. 使用函數來替換元素

1.4.1. replaceWith(function(index, origHtml))方法使用函數替換所有匹配元素指定坐標的元素

1.4.2. 語法

$(selector).replaceWith(function(index, origHtml))

1.4.3. 參數

1.5. 如果replaceWith()方法用于已有元素, 這些元素會被從當前位置移走, 然后替換匹配元素。

1.6. 例子

1.6.1. 代碼

<!DOCTYPE html> <html><head><meta charset="utf-8" /><title>replaceWith替換元素</title><script type="text/javascript" src="jquery.js"></script><script type="text/javascript">$(document).ready(function(){$('#btn1').click(function(){$("p").replaceWith("<p>語法<br />$(selector).replaceWith(content)</p>");});$('#btn2').click(function(){$("p").replaceWith(function(index, origHtml) {return "<p>語法<br />$(selector).replaceWith(function(index, origHtml))</p>";});});$('#btn3').click(function(){$("p").replaceWith($('h2'));});});</script></head><body><h2><i>replaceWith()</i>方法替換元素</h2><div style="background-color: red; width: 600px; height: 100px;"><p>replaceWith(content)方法用指定的html內容或元素(元素本身和子元素)替換每一個匹配元素。</p></div><div style="background-color: green; width: 600px; height: 100px;"><p>replaceWith(function(index, origHtml))方法使用函數替換所有匹配元素中指定坐標的元素。</p></div> <br /><button id="btn1">replaceWith替換元素</button> <button id="btn2">replaceWith使用函數來替換元素</button><button id="btn3">使用已存在元素替換</button></body> </html>

1.6.2. 效果圖

2. replaceAll()方法

2.1. replaceAll()方法用指定的html內容或元素(元素本身和子元素)替換每一個匹配元素

2.2. replaceAll()與replaceWith()作用相同。差異在于語法: 內容和選擇器的位置, 以及replaceWith()能夠使用函數進行替換。

2.3. 語法

$(content).replaceAll(selector)

2.4. 參數

2.5. 例子

2.5.1. 代碼

<!DOCTYPE html> <html><head><meta charset="utf-8" /><title>replaceAll替換元素</title><script type="text/javascript" src="jquery.js"></script><script type="text/javascript">$(document).ready(function(){$('#btn1').click(function(){var txt = document.createElement("p");txt.innerHTML = "以DOM創建新元素.";$(txt).replaceAll("p");});});</script></head><body><div style="background-color: red; width: 600px; height: 100px;"><p>replaceAll()方法用指定的html內容或元素(元素本身和子元素)替換每一個匹配元素。</p></div> <div style="background-color: green; width: 600px; height: 100px;"><p>replaceAll()與replaceWith()作用相同。差異在于語法: 內容和選擇器的位置, 以及replaceWith()能夠使用函數進行替換。</p></div><br /><button id="btn1">replaceAll替換元素</button></body> </html>

2.5.2. 效果圖

總結

以上是生活随笔為你收集整理的013_替换元素的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。