Javascript jQuery附加到父问题
首先,也最重要的是,提前感谢您的帮助 我试图在页面上找到的每个锚定标记的上方放置一个悬停的Javascript jQuery附加到父问题,javascript,jquery,appendto,Javascript,Jquery,Appendto,首先,也最重要的是,提前感谢您的帮助 我试图在页面上找到的每个锚定标记的上方放置一个悬停的divovertop。所以我得到偏移量,找到每个元素的高度和宽度,然后创建一个新的div,并将CSS设置为这些测量值。将新创建的DIV附加到锚标记的父级的代码有问题 $( "a" ).each(function( index ) { tempoffset = $( this ).offset(); tempheight = $( this ).height(); tempwidth
div
overtop。所以我得到偏移量,找到每个元素的高度和宽度,然后创建一个新的div,并将CSS设置为这些测量值。将新创建的DIV附加到锚标记的父级的代码有问题
$( "a" ).each(function( index ) {
tempoffset = $( this ).offset();
tempheight = $( this ).height();
tempwidth = $( this ).width();
tempoverlay = document.createElement('div');
tempoverlay.appendTo($(this).parent());
tempoverlay.setAttribute("style","background: #ccc; position: fixed; height: " + tempheight + "px; width: " + tempwidth + "px; left: " + tempoffset.left + "px; top: " + tempoffset.top + "px;");
});
我得到的错误是
tempoverlay.appendTo不是函数
对这里可能发生的事情有什么想法吗
tempoverlay
是本机DOM节点,而不是jQuery对象,因为这是document.createElement
创建的对象,因此它没有appendTo
方法
改为创建jQuery对象
var tempoverlay = $('<div />');
tempoverlay.appendTo( $(this).parent() )
.css({
background : '#ccc',
position : 'fixed',
height : tempheigh,
width : tempwidth,
left : tempoffset.left,
top : tempoffset.top
});
var tempoverlay=$('');
tempoverlay.appendTo($(this.parent())
.css({
背景:“#ccc”,
位置:'固定',
身高:tempheigh,
宽度:tempwidth,
左:tempoffset.left,
顶部:tempoffset.top
});
尝试改用append()
,因为tempoverlay
不是jquery对象,您不能调用jquery函数。在dom节点上的appendTo
:
$( "a" ).each(function( index ) {
tempoffset = $( this ).offset();
tempheight = $( this ).height();
tempwidth = $( this ).width();
tempoverlay = document.createElement('div');
tempoverlay.setAttribute("style","background: #ccc; position: fixed; height: " + tempheight + "px; width: " + tempwidth + "px; left: " + tempoffset.left + "px; top: " + tempoffset.top + "px;");
$(this).parent().append(tempoverlay);
});
希望这有帮助。
tempoverlay
不是jQuery元素。它是一个dom元素
尝试以下操作:$(tempoverlay).appendTo…