[英]button change name when pressed
I'm trying to create a button that changes its value when it is pressed. 我正在尝试创建一个按钮,该按钮在按下时会更改其值。
$("button").click( function() {
valor = this.html();
retorno = (valor == 'Exibir') ? 'Ocultar' : 'Exibir';
this.html(retorno);
});
I'm having this message: 我收到此消息:
Uncaught TypeError: Object # has no method 'html' 未捕获的TypeError:对象#没有方法'html'
I appreciate any help. 感谢您的帮助。
this
是一个HTMLElementNode对象,而不是jQuery对象。
valor = jQuery(this).html();
this
refers to the HTMLElementNode (window) object and is not an jquery object and as html is a jquery method it won't work. this
是指HTMLElementNode(窗口)对象,而不是jquery对象,而html是jquery方法,因此它将不起作用。
So to make it a jquery object you need to wrap the $
around it: 因此,要使其成为jquery对象,您需要在其周围包装
$
:
$("button").click( function() {
valor = $(this).html();
retorno = (valor == 'Exibir') ? 'Ocultar' : 'Exibir';
$(this).html(retorno);
});
尝试这个
valor = $(this).html();
this
将引用HTMLElementNode,因此您可以执行valor = this.innerHTML
并避免jQuery调用。
$("button").click( function() {
valor = $(this).html();
retorno = (valor == 'Exibir') ? 'Ocultar' : 'Exibir';
$(this).html(retorno);
});
用这个 :
valor = jQuery(this).html();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.