admin管理员组文章数量:1295928
I have the following HTML:
<a class="small" href="/ments/new">Write New Comment</a>
And the following js:
$(document).ready(function () {
$('ment > a').click( function (event) {
var url = event.target.href
$('ment_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?
I have the following HTML:
<a class="small" href="/ments/new">Write New Comment</a>
And the following js:
$(document).ready(function () {
$('.ment > a').click( function (event) {
var url = event.target.href
$('.ment_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?
Share asked Jan 28, 2014 at 19:52 reectrixreectrix 8,62921 gold badges59 silver badges84 bronze badges 3-
Please read the jQuery tutorial about basic event handling: "In addition to the event object, the event handling function also has access to the DOM element that the handler was bound to via the keyword
this
. To turn the DOM element into a jQuery object that we can use jQuery methods on, we simply do$( this )
". Tutorials are great, they help a lot! You should read them! – Felix Kling Commented Jan 28, 2014 at 19:58 - From everything I've been reading, it's kind of ambiguous when to use event.target, vs $(this)...since $(this) could also refer to the surrounding DOM element, whereas event.target refers to the element that was actually clicked on. – reectrix Commented Jan 28, 2014 at 20:22
-
this
refers to the element the handler is bound to (unless you use jQuery's event delegation mechanism). In any case, this page also mentions thatevent.target
is "the DOM element that initiated the event". And the part I quoted says that we pass DOM elements (e.g.this
) to$
(e.g.$(this)
), to create a jQuery object. I'm just saying that all the information is there, you just have to put the pieces together :) – Felix Kling Commented Jan 28, 2014 at 21:15
5 Answers
Reset to default 4You could simply use this
.
$('.ment > a').click( function (event) {
$(this).text("");
//Other stuff
});
You don't have to use event.target
this
refers to the element which you have attached the event. In your case, $('.ment > a')
If you have multiple anchor links directly under the container .ment
, it helps to differentiate between them & you could do specific things with each of those anchor links.
To set the text to blank use this method .text('')
.
So, you method would change to the following one:
$(document).ready(function () {
$('.ment > a').click( function (event) {
var url = event.target.href;
// this can be replaced by the following
var url = $(this).attr('href');
$('.ment_area').load(url);
$(this).text(''); // empty the current string
event.preventDefault();
});
});
It is better practice to add ;
at the end of the code lines! :)
You're using jQuery so why stick with Javascript alone?
$(document).ready(function () {
$('.ment > a').click( function (event) {
$this = $(this);
var url = $this.attr('href');
$('.ment_area').load(url)
$this.text("");
event.preventDefault()
})
})
This will save you the headaches of different event structure in different browsers
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.
That's why using currentTarget
is safer since it is the element on which the event is binded.
this
by default is the same as currentTarget
but this value can be altered by .apply()
, .call()
or .bind
.
Do you mean so it disappears? You could do:
$(this).hide();
or
$(this).text("");
本文标签: javascriptSetting the valuetext of eventtarget with jQueryStack Overflow
版权声明:本文标题:javascript - Setting the valuetext of event.target with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741595132a2387365.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论