Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.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 JS函数:无法读取属性';数据集';未定义的_Javascript_Jquery - Fatal编程技术网

Javascript JS函数:无法读取属性';数据集';未定义的

Javascript JS函数:无法读取属性';数据集';未定义的,javascript,jquery,Javascript,Jquery,我有一个JS函数,可以用Youtube iFrame替换父节点: function labnolIframe() { var iframe = document.createElement("iframe"); iframe.setAttribute("src", "//www.youtube.com/embed/" + this.parentNode.dataset.id + "?autoplay=1&autohide=2&border=0&wmode=

我有一个JS函数,可以用Youtube iFrame替换父节点:

function labnolIframe() {
    var iframe = document.createElement("iframe");
    iframe.setAttribute("src", "//www.youtube.com/embed/" + this.parentNode.dataset.id + "?autoplay=1&autohide=2&border=0&wmode=opaque&enablejsapi=1&controls=1&showinfo=0");
    iframe.setAttribute("frameborder", "0");
    iframe.setAttribute("id", "youtube-iframe");
    iframe.setAttribute("width", "100%");
    iframe.setAttribute("allowFullscreen", "1");
    this.parentNode.replaceChild(iframe, this);
}
我在页面加载时动态创建div,该div具有运行上述函数的onclick属性:

(function() {
    var v = document.getElementsByClassName("youtube-player");
    for (var n = 0; n < v.length; n++) {
        var p = document.createElement("div");
        p.innerHTML = labnolThumb(v[n].dataset.id);
        p.onclick = labnolIframe;
        v[n].appendChild(p);
    }
})();
当我单击.play图标时,我想运行用youtube Iframe替换父div的代码。但是我发现上面函数中的“this”并不是指DOM中的“this”元素(与我在第一个示例中调用它时不同)


如何更改代码,使其在上一个示例中引用“this”?

若要在
上调用click函数,请使用play icon
元素使用其父节点的值来调用此在调用替换函数时,可以使用方法指定所需的
this
值。如果正确,这是父节点:

$('.play-icon').click(function(){
   labnolIframe.call( this.parentNode); 
});

要从
.play icon
访问
对象,您需要在
单击
事件上直接调用函数
labnolIframe
,而不是在
单击
事件中调用它

更改:

$('.play-icon').click(function(){
   labnolIframe();
});
致:


这里有一个例子:

请告知DOM中
元素和iframe元素之间的关系。播放图标
元素?您的标题包含“无法读取未定义的属性'dataset'。添加一些html标记,以便我们能够发现具有类
youtube player
play icon
的元素。javascript函数中的“这个”将引用文档对象(DOM)。如果要将“.play icon”对象传递给“labnolIframe”,则必须将其作为参数或变量传递$('.play icon')。单击(function(){labnolIframe(this);});
$('.play-icon').click(function(){
   labnolIframe();
});
$('.play-icon').click(labnolIframe);