我是jQuery的新手,所以我很感激您提供的任何帮助/建议!
我想做什么: 基本上,当你将鼠标悬停在链接上时,我只需要链接的alt来替换父div的描述文本(位于单独的div中)。我已经让这个单行工作,但是当我有多行多行时,我遇到了问题。我似乎无法让脚本获取父元素描述,也不能用悬停文本中的alt替换它。 jsfiddle sample
请解释一下我做错了什么。
我的HTML:
$(function() {
"use strict";
$("#description").text(id.desc);
$("a.description").hover(
function() {
$("#description").text($(this).attr("desc"));
}
);
});

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div id="1" desc="Div Description for div 1" class="row">
<div class="col-sm-4">
<ul>
<li>
<h3>Link set 1-1</h3>
</li>
<li><a id="link1-1" desc="Sample Alt Text 1-1" href="/">Link </a>
</li>
<li><a id="link1-2" desc="Sample Alt Text 1-2" href="/">Link </a>
</li>
<li><a id="link1-3" desc="Sample Alt Text 1-3" href="/">Link </a>
</li>
<li><a id="link1-4" desc="Sample Alt Text 1-4" href="/">Link </a>
</li>
</ul>
</div>
<div class="col-sm-4">
<ul>
<li>
<h3>Link set 1-2</h3>
</li>
<li><a id="link1-51" desc="Sample Alt Text 1-5" href="/">Link </a>
</li>
<li><a id="link1-6" desc="Sample Alt Text 1-6" href="/">Link </a>
</li>
<li><a id="link1-7" desc="Sample Alt Text 1-7" href="/">Link </a>
</li>
<li><a id="link1-8" desc="Sample Alt Text 1-8" href="/">Link </a>
</li>
</ul>
</div>
<div class="col-sm-4">
<div id="description"></div>
</div>
</div>
<div id="2" desc="Div Description for div 2" class="row">
<div class="col-sm-4">
<ul>
<li>
<h3>Link set 2-1</h3>
</li>
<li><a id="link2-1" desc="Sample Alt Text 2-1" href="/">Link </a>
</li>
<li><a id="link2-2" desc="Sample Alt Text 2-2" href="/">Link </a>
</li>
<li><a id="link2-3" desc="Sample Alt Text 2-3" href="/">Link </a>
</li>
<li><a id="link2-4" desc="Sample Alt Text 2-4" href="/">Link </a>
</li>
</ul>
</div>
<div class="col-sm-4">
<ul>
<li>
<h3>Link set 2-2</h3>
</li>
<li><a id="link2-5" desc="Sample Alt Text 2-5" href="/">Link </a>
</li>
<li><a id="link2-6" desc="Sample Alt Text 2-6" href="/">Link </a>
</li>
<li><a id="link2-7" desc="Sample Alt Text 2-7" href="/">Link </a>
</li>
<li><a id="link2-8" desc="Sample Alt Text 2-8" href="/">Link </a>
</li>
</ul>
</div>
<div class="col-sm-4">
<div id="description"></div>
</div>
</div>
</div>
&#13;
答案 0 :(得分:3)
http://jsbin.com/sosigi/edit?html,css,js,output
id=description
。 a.description
和根本没有 <a class="description"
。 alt
是一个无效的 A
元素属性,而且desc
对于HTML的属性列表是未知的。 使用有效的data-*
属性和正确的选择器
jQuery(function( $ ) {
$("[data-desc]").hover(function(e) {
var mEnter = e.type==="mouseenter";
var $descTerget = $(this).closest(".row").find(".description");
$descTerget.text( mEnter ? $(this).data("desc") : "");
});
});
&#13;
@import url('http://getbootstrap.com/dist/css/bootstrap.css');
&#13;
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="container">
<div id="div1"class="row">
<div class="col-sm-4">
<ul>
<li>
<h3>Link set 1-1</h3>
</li>
<li><a id="link1-1" data-desc="Sample Alt Text 1-1" href="/">Link </a>
</li>
<li><a id="link1-2" data-desc="Sample Alt Text 1-2" href="/">Link </a>
</li>
<li><a id="link1-3" data-desc="Sample Alt Text 1-3" href="/">Link </a>
</li>
<li><a id="link1-4" data-desc="Sample Alt Text 1-4" href="/">Link </a>
</li>
</ul>
</div>
<div class="col-sm-4">
<ul>
<li>
<h3>Link set 1-2</h3>
</li>
<li><a id="link1-5" data-desc="Sample Alt Text 1-5" href="/">Link </a>
</li>
<li><a id="link1-6" data-desc="Sample Alt Text 1-6" href="/">Link </a>
</li>
<li><a id="link1-7" data-desc="Sample Alt Text 1-7" href="/">Link </a>
</li>
<li><a id="link1-8" data-desc="Sample Alt Text 1-8" href="/">Link </a>
</li>
</ul>
</div>
<div class="col-sm-4">
<div class="description"></div>
</div>
</div>
<div id="div2" class="row">
<div class="col-sm-4">
<ul>
<li>
<h3>Link set 2-1</h3>
</li>
<li><a id="link2-1" data-desc="Sample Alt Text 2-1" href="/">Link </a>
</li>
<li><a id="link2-2" data-desc="Sample Alt Text 2-2" href="/">Link </a>
</li>
<li><a id="link2-3" data-desc="Sample Alt Text 2-3" href="/">Link </a>
</li>
<li><a id="link2-4" data-desc="Sample Alt Text 2-4" href="/">Link </a>
</li>
</ul>
</div>
<div class="col-sm-4">
<ul>
<li>
<h3>Link set 2-2</h3>
</li>
<li><a id="link2-5" data-desc="Sample Alt Text 2-5" href="/">Link </a>
</li>
<li><a id="link2-6" data-desc="Sample Alt Text 2-6" href="/">Link </a>
</li>
<li><a id="link2-7" data-desc="Sample Alt Text 2-7" href="/">Link </a>
</li>
<li><a id="link2-8" data-desc="Sample Alt Text 2-8" href="/">Link </a>
</li>
</ul>
</div>
<div class="col-sm-4">
<div class="description"></div>
</div>
</div>
</div>
</body>
</html>
&#13;