如需刪除元素和內(nèi)容,一般可使用以下兩個(gè) jQuery 方法:

我們提供的服務(wù)有:網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、章丘ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的章丘網(wǎng)站制作公司
remove() - 刪除被選元素(及其子元素)
empty() - 從被選元素中刪除子元素
本文實(shí)例講述了jQuery使用empty()方法刪除元素及其所有子元素的方法,分享給大家供大家參考,具體實(shí)現(xiàn)方法如下:
$.ajax({
url: "SearchSN.aspx",
data: "SN=" + $("#txtStorageSN").val(),
cache: false,
success: function (html) {
$("#showResult").empty();
$("#showResult").append(html);
$("#showResult").css("color", "red");
}
});
頁面:div id="showResult"/div
$("#showResult").empty();//清除div內(nèi)容(Div內(nèi)容是通過aspx頁面進(jìn)行動(dòng)態(tài)添加的,需要先清除div內(nèi)容)
jquery能直接取出所有的div,它的text()方法可以用來清空div。那么可以這樣寫:$("div").text("");這樣就清空的所有的div內(nèi)容了
可以使用?jQuery 設(shè)置內(nèi)容的val()方法來實(shí)現(xiàn),設(shè)置內(nèi)容為空即達(dá)到清空文本框內(nèi)容的目的:
$("input[name='test']").val("").focus();?//?將name=test的文本框清空并獲得焦點(diǎn),以便重新輸入
示例代碼如下
創(chuàng)建Html元素
div?class="box"
span點(diǎn)擊按鈕后清除文本框內(nèi)容:/spanbr
div?class="content"
input?type="text"?name="test"?value="這是默認(rèn)的內(nèi)容"
/div
input?type="button"?class="btn"?value="清除文本框內(nèi)容"
/div
設(shè)置css樣式
div.box{width:300px;height:250px;padding:10px?20px;margin:20px;border:4px?dashed?#ccc;}
div.boxspan{color:#999;font-style:italic;}
div.content{width:250px;height:100px;margin:10px?0;padding:5px?20px;border:2px?solid?#ff6666;}
input[type='text']{width:200px;height:30px;border:none;}
input[type='button']{width:120px;height:30px;margin:10px;border:2px?solid?#ebbcbe;}
編寫jquery代碼
$(function(){
$("input:button").click(function()?{
$("input[name='test']").val("").focus();?//?清空并獲得焦點(diǎn)
});
})
觀察效果
初始狀態(tài)
點(diǎn)擊按鈕清空文本框后
可以使用Jquery中的siblings()和remove()方法實(shí)現(xiàn)。實(shí)現(xiàn)原理主要是使用siblings獲取被點(diǎn)擊元素之外的同級元素,然后使用remove()刪除。完整的代碼如下:
運(yùn)行的效果如下:
例如當(dāng)點(diǎn)擊DIV內(nèi)容為2的框時(shí),其他的DIV被清除,內(nèi)容為2的框移到最頂,點(diǎn)擊后的效果圖如下:
擴(kuò)展資料:
如果想對移到最頂?shù)目蜻M(jìn)行一定的突出顯示處理,比如給個(gè)背景顏色,字體加大,可以利用css()方法,代碼如下:
!doctype html
html
head
meta charset="utf-8"
titleJQuery例子/title
link href="__CSS__/base.css" rel="stylesheet"
script src="__JS__/jquery.min.js"/script
/head
body
script
$(function(){
$(".class1").click(function() {
$(this).siblings().remove();
$(this).css({"background-color":"yellow","font-size":"50px"});
});
});
/script
div class="main"
div class="class1"1/div
div class="class1"2/div
div class="class1"3/div
div class="class1"4/div
/div
style type="text/css"
.main{width:640px;height:500px;margin:0 auto;border:1px solid red;}
.class1{width:90%;height:100px;margin:0 auto;border:1px solid red;margin-top:10px;font-size:40px;}
/style
/body
/html
運(yùn)行的效果如下:
jquery 提供了remove() 方法,用來移除被選元素,包括所有文本和子節(jié)點(diǎn)。因此只需根據(jù)添加的div的id即可將其刪除:
$(div_id).remove();
實(shí)例演示如下:
HTML結(jié)構(gòu)
input?type="button"?value="刪除"
div?id="test"這是示例的DIV/div
jquery代碼
$(function(){?? $("input[type='button']").click(function()?{????????$("div#test").remove();?? });
網(wǎng)站題目:jquery清空div內(nèi)容,jquery清空html內(nèi)容
URL分享:http://chinadenli.net/article46/dsipgeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、小程序開發(fā)、企業(yè)建站、域名注冊、營銷型網(wǎng)站建設(shè)、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)