Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

javascript - 如何使用JavaScript清除div的内容? [关闭](How do I clear the content of a div using JavaScript? [closed])

When the user clicks a button on my page, the content of a div should be cleared.

(当用户单击页面上的按钮时,应清除div的内容。)

How would I go about accomplishing this?

(我将如何实现这一目标?)

  ask by Rajasekar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just Javascript (as requested)

(只是Javascript(根据要求))

Add this function somewhere on your page (preferably in the <head> )

(在页面的某处添加此功能(最好在<head> ))

function clearBox(elementID)
{
    document.getElementById(elementID).innerHTML = "";
}

Then add the button on click event:

(然后在点击事件上添加按钮:)

<button onclick="clearBox('cart_item')" />

In JQuery (for reference)

(在JQuery中(供参考))

If you prefer JQuery you could do:

(如果你更喜欢JQuery,你可以这样做:)

$("#cart_item").html("");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...