javascript 使用 jQuery 设置 event.target 的值/文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21415436/
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
Setting the value/text of event.target with jQuery
提问by reectrix
I have the following HTML:
我有以下 HTML:
<a class="small" href="/comments/new">Write New Comment</a>
And the following js:
以及以下js:
$(document).ready(function () {
$('.comment > a').click( function (event) {
var url = event.target.href
$('.comment_area').load(url)
**event.target.text = ""**
event.preventDefault()
})
})
I am trying to set the text of the link to blank, but can't seem to find a way to do this. I've also tried:
我试图将链接的文本设置为空白,但似乎无法找到一种方法来做到这一点。我也试过:
event.target.text("")
event.target.value = ""
and none of these work. Is this even possible to do from the JavaScript side?
这些都不起作用。这甚至可以从 JavaScript 方面做到吗?
回答by Krishna
You could simply use this
.
你可以简单地使用this
.
$('.comment > a').click( function (event) {
$(this).text("");
//Other stuff
});
You don't have to use event.target
你不必使用 event.target
this
refers to the element which you have attached the event. In your case, $('.comment > a')
this
指的是您附加了事件的元素。在你的情况下,$('.comment > a')
If you have multiple anchor links directly under the container .comment
, it helps to differentiate between them & you could do specific things with each of those anchor links.
如果您在容器正下方有多个锚链接.comment
,则有助于区分它们并且您可以对每个锚链接执行特定操作。
回答by Afzaal Ahmad Zeeshan
To set the text to blank use this method .text('')
.
要将文本设置为空白,请使用此方法.text('')
。
So, you method would change to the following one:
因此,您的方法将更改为以下方法:
$(document).ready(function () {
$('.comment > a').click( function (event) {
var url = event.target.href;
// this can be replaced by the following
var url = $(this).attr('href');
$('.comment_area').load(url);
$(this).text(''); // empty the current string
event.preventDefault();
});
});
It is better practice to add ;
at the end of the code lines! :)
;
在代码行的末尾添加是更好的做法!:)
回答by mcolo
Do you mean so it disappears? You could do:
你的意思是这样它就消失了?你可以这样做:
$(this).hide();
or
或者
$(this).text("");
回答by Riccardo Zorn
You're using jQuery so why stick with Javascript alone?
您正在使用 jQuery,那么为什么要单独使用 Javascript?
$(document).ready(function () {
$('.comment > a').click( function (event) {
$this = $(this);
var url = $this.attr('href');
$('.comment_area').load(url)
$this.text("");
event.preventDefault()
})
})
This will save you the headaches of different event structure in different browsers
这将为您省去不同浏览器中不同事件结构的麻烦
回答by Karl-André Gagnon
Try :
尝试 :
event.target.innerHTML = "";
or
或者
event.currentTarget.innerHTML = "";
or
或者
this.innerHTML = "";
event.target
is the very first element on the top of the DOM. It mean that if there is a span in your a
, the span will be selected.
event.target
是 DOM 顶部的第一个元素。这意味着如果您a
的 中有跨度,则将选择该跨度。
That's why using currentTarget
is safer since it is the element on which the event is binded.
这就是为什么 usingcurrentTarget
更安全,因为它是绑定事件的元素。
this
by default is the same as currentTarget
but this value can be altered by .apply()
, .call()
or .bind
.
this
默认情况下与 相同,currentTarget
但可以通过.apply()
,.call()
或更改此值.bind
。