jQuery - 选择所有 <span> 元素并删除它们的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7523540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:58:18  来源:igfitidea点击:

jQuery - select all <span> elements and remove their text

jqueryjquery-selectors

提问by Singular1ty

Hello guys, I have spent about twenty minutes searching in vain for my answer, and need your help. There are thousands of requests for help on how to select elements with jQuery - but everyone wants to do with with some kind of condition, ie, only select an anchor with a certain ID at a certain Y position on the page.

Hello guys,我花了大约二十分钟的时间寻找我的答案是徒劳的,需要你的帮助。关于如何使用 jQuery 选择元素的帮助请求数以千计——但每个人都想处理某种条件,即,只在页面上的特定 Y 位置选择具有特定 ID 的锚点。

I have a simple request. How do I select all<span>elements on my page and remove their text?

我有一个简单的要求。如何选择页面上的所有<span>元素并删除它们的文本?

See the thing is, I have a form, and I have <spans>. When I click the Clear Button input, all fields revert back to default (of course). But I want the spanelements to have their text deleted.

看到的是,我有一个表格,我有<spans>。当我单击清除按钮输入时,所有字段都恢复为默认值(当然)。但我希望span元素的文本被删除。

The <html>on it is simple:

<html>它很简单:

<input type="reset" value="Clear form" name="Clear Button" class="clear">

And my jQuery:

还有我的 jQuery:

/* Clear form - used to revert all Spans back to normal */
$('#Clear Button').click(function(){
        $('span').val('');

});

So, the Reset effect works because that's DOM/HTML. But my jQuery is sadly broken.

因此,重置效果有效,因为那是 DOM/HTML。但是我的 jQuery 很遗憾地坏了。

Can anyone tell me what's going wrong? My script is after the Button declaration, if that helps.

谁能告诉我出了什么问题?如果有帮助,我的脚本在 Button 声明之后。

回答by Joseph Silber

Your problem is with your button selector. You are not selecting your button. Use this instead:

您的问题出在按钮选择器上。你没有选择你的按钮。改用这个:

/* Clear form - used to revert all Spans back to normal */
$('input[name="Clear Button"]').click(function(){
        $('span').text('');
});

Or use $('input[type="reset"]')if that's the only one there...

或者$('input[type="reset"]')如果那是唯一的使用...

回答by ChaseTonyReid

Did you source the jquery.js file into your code, and make sure when your refering to the span instead of leaving it "" (blank) use the function $('span').hide Hope i helped

您是否将 jquery.js 文件源到您的代码中,并确保当您引用跨度而不是将其保留为“”(空白)时使用函数 $('span').hide 希望我有所帮助