[英]Change inner HTML on hover
I'm very new to jQuery and Javascript development. 我对jQuery和Javascript开发非常陌生。 I've quickly cooked up an idea for a site navigation that utilizes html data tags to generate link descriptions.
我很快就提出了利用html数据标签生成链接描述的站点导航的想法。 I've got it set up in a fiddle here .
我已经在这里摆好了 。 My problem is that the jquery is not functioning as intended.
我的问题是,jQuery无法按预期运行。 I am too new to identify what the error may be.
我太新了,无法确定错误可能是什么。 I would greatly appreciate any tips.
我将不胜感激任何提示。 Thank you in advance!
先感谢您!
Here is the code snippet for the jquery: 这是jquery的代码片段:
$(document).ready(function () {
$(".nav-button").hover(function (e) {
var description = this.data('title') + ' <span>' + this.data('description') + '</span>';
document.getElementById('nav-description').innerHTML = description;
}, function (e) {
document.getElementById('nav-description').innerHTML = '';
});
});
You're not wrapping this
as a jQuery object. 你不是包装
this
是一个jQuery对象。
var description = $(this).data('title') + ' <span>' + $(this).data('description') + '</span>';
^^^
While you're at it, might as well use jQuery for the other line as well. 当您使用它时,最好也将jQuery用于另一行。
$('#nav-description').html(description);
jQuery way should look like this: jQuery的方式应如下所示:
$(function () {
$(".nav-button").hover(function (e) {
var description = $(this).data('title') + ' <span>' + $(this).data('description') + '</span>';
$('#nav-description').html(description);
}, function (e) {
$('#nav-description').empty();
});
});
Recommendations: 建议:
Don't mesh native javascript getElementById()
and so on with jQuery selectors. 不要将本机javascript
getElementById()
等与jQuery选择器啮合。
Use $(function () {});
使用
$(function () {});
patterns instead of document.ready
. 模式,而不是
document.ready
。
this
in the scope of the jQuery event handlers refers to the DOM element and not a jQuery object. this
在jQuery事件处理程序的范围内是指DOM元素,而不是jQuery对象。 So you need to wrap this
with $
to give you the jQuery version ie $(this)
. 因此,您需要使用
$
来包装this
,以提供jQuery版本,即$(this)
。
The way jQuery works you should find the following is true $(this)[0] === this
. jQuery的工作方式应该是正确的
$(this)[0] === this
。 ie the jQuery object will have an array of DOM elements that were matched by the selector. 即jQuery对象将具有由选择器匹配的DOM元素数组。
I think because you're new to jQuery this might help.... 我认为,因为您不熟悉jQuery,这可能会有所帮助。
http://www.learningjquery.com/2007/08/what-is-this/ http://www.learningjquery.com/2007/08/what-is-this/
this is a DOM element
这是一个DOM元素
When is this a DOM element?
这是什么时候的DOM元素? The short answer is - usually.
简短的答案是-通常。 Scripting with jQuery quite often involves the use of callback functions.
用jQuery编写脚本经常涉及回调函数的使用。 Whether handling an event, iterating a collection of nodes, animating an image, or applying a dynamic filter, callbacks are used to invoke your custom code at the appropriate time.
无论是处理事件,对节点的集合进行迭代,对图像进行动画处理还是应用动态过滤器,回调均用于在适当的时候调用您的自定义代码。 To make things easy for you, jQuery sets the scope of the callback function to the element which is the subject of the callback.
为了使您更轻松,jQuery将回调函数的范围设置为作为回调主题的元素。
... and a rewrite of your code ...并重写您的代码
$(document).ready(function () {
$(".nav-button").hover(function (e) {
var $this = $(this),
description = $this.data('title') + ' <span>' + $this.data('description') + '</span>';
$('#nav-description').html(description);
}, function (e) {
$('#nav-description').html('');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.