我正在尝试自己的工具提示,它应该显示父元素名称值的值:
我在我的锚标签后附加新div,并尝试绑定父锚名称值的文本值...
但我在所有(.tooltipContainer)div中只得到姓氏值(工具提示3333333333333)?任何人都可以帮忙解决?
HTML:
<!-- tooltip - begin -->
<ul>
<li><a name="tooltip 1111111111111" class="customToolTip">tool tip 1111</a></li>
<li><a name="tooltip 2222222222222" class="customToolTip">tool tip 222</a></li>
<li><a name="tooltip 3333333333333" class="customToolTip">tool tip 333333</a></li>
</ul>
<!-- tooltip end -->
JS:
$(document).ready( function() {
$(".customToolTip").each(function(i){
var customToolTipName = $(this).attr('name');
var parentAttrNameValue = $(this).parent().find(".customToolTip").attr('name');
// alert(parentAttrNameValue);
$(this).after('<div class="tooltipContainer"></div>');
$(".tooltipContainer").html(parentAttrNameValue);
});
});
答案 0 :(得分:0)
问题是$(".tooltipContainer")
获取了类tooltipContainer
的所有元素而不是刚刚添加的元素,并将其html设置为parentAttrNameValue
值,因此当处理最后一项时,其值为为所有元素设置。
相反,您可以添加下面给出的元素,这里我们在创建新元素时设置html。
$(document).ready(function () {
$(".customToolTip").each(function (i) {
var customToolTipName = $(this).attr('name');
var parentAttrNameValue = $(this).parent().find(".customToolTip").attr('name');
// alert(parentAttrNameValue);
$('<div />', {
html: parentAttrNameValue, 'class': 'tooltipContainer'
}).insertAfter(this);
});
});
演示:Fiddle
还不确定parentAttrNameValue
是什么,它与customToolTipName