[英]Why is this hover toggle jQuery not working?
I have separate jQuery functions for "mouseenter" and "mouseleave" that work fine: 对于“ mouseenter”和“ mouseleave”,我有单独的jQuery函数,它们可以正常工作:
$('#imgPostTravel').mouseenter(function () {
$('#imgPostTravel').addClass('popout_image');
$('#imgPostTravel').addClass('shadow');
});
$('#imgPostTravel').mouseleave(function () {
$('#imgPostTravel').removeClass('popout_image');
$('#imgPostTravel').removeClass('shadow');
});
...but am hoping to consolidate it into one "hover" toggle operation. ...但希望将其合并为一个“悬停”切换操作。
I first want to make sure it really works, so tried the following on a jsfiddle here : 我首先要确保它确实有效,因此在这里在jsfiddle上尝试了以下方法:
$( "ptVerbiage" ).hover(function() {
$(this).val('yep!');
}, function() {
$(this).val('nope!');
});
I've tried several things besides setting the value of "val" (changing the "disabled" attr, changing the color, backgroundcolor, etc.) but none of them does a thing. 除了设置“ val”的值(更改“ disabled” attr,更改颜色,backgroundcolor等)外,我还尝试了其他几种方法,但是它们都不起作用。 Is this the wrong way to hover/toggle, or what's wrong?
这是错误的悬停/切换方式,还是出了什么问题?
You forgot the hashtag to make reference to an ID. 您忘记了标签以引用ID。 Also, your target element is a
h2
, that has no .val()
method because it is not a form (text) input. 另外,您的目标元素是
h2
,它没有.val()
方法,因为它不是表单(文本)输入。 You have to use .text()
instead. 您必须改用
.text()
。
The portion of code should look like this ( jsFiddle ): 代码部分应如下所示( jsFiddle ):
$("#ptVerbiage").hover(function() {
$(this).text('yep!');
}, function() {
$(this).text('nope!');
});
You seem to be missing a # 您似乎缺少一个#
$("ptVerbiage")
=> $("#ptVerbiage")
$("ptVerbiage")
=> $("#ptVerbiage")
AND 和
not .val()
but .text()
; 不是
.val()
而是.text()
; as .val is for inputs 因为.val用于输入
should look like this 应该看起来像这样
$( "#ptVerbiage" ).hover(function() {
$(this).text('yep!');
}, function() {
$(this).text('nope!');
});
http://jsfiddle.net/n9sq7x8y/4/ http://jsfiddle.net/n9sq7x8y/4/
Using everyone's suggestions, this is what works: 使用每个人的建议,这是可行的:
$( "#imgPostTravel" ).hover(function() {
$(this).addClass('popout_image');
$(this).addClass('shadow');
}, function() {
$(this).removeClass('popout_image');
$(this).removeClass('shadow');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.