Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery附加到父问题_Javascript_Jquery_Appendto - Fatal编程技术网

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…