2017-08-27 54 views
0

我想[标题] 这是我的肆意代码:在另一个元素克隆的属性值的一部分的另一个属性 - JS

<script> 
$(document).ready(function(){ 
    var thmbElt = document.getElementsByClassName("imgbox")[0]{var vl1 = $(this).attr("src");}; 
    $(.h_iframe-aparat_embed_frame).attr("src",function{ 
     return "https://www.example.com/video/video/embed/videohash/"+vl1.substr(51, 56)+"/vt/frame"; 
}); 
</script> 

那结果是:没有什么

+0

是否定义了vl1或未定义? – Nikolaus

回答

0

如果我没有理解正确地说,你想获得一个元素的src,并在另一个元素中使用它的一部分。以下是样本:

<img src="https://images.pexels.com/photos/34950/pexels-photo.jpg?h=350&auto=compress&cs=tinysrgb" 
class="imgbox" width="80px" height="80px" /> 
<img src="https://images.pexels.com/photos/34950/pexels-photo.jpg?h=350&auto=compress&cs=tinysrgb" 
class="imgbox" width="80px" height="80px" /> 

<script> 
    $(document).ready(function(){ 
     var thmbElt = document.getElementsByClassName("imgbox")[0]; 
     var thmbElt1 = document.getElementsByClassName("imgbox")[1]; //Change this line to get img src from another field 

     var attr = thmbElt1.getAttribute("src"); 
     var attrSubset = "https://www.example.com/video/video/embed/videohash/" + attr.substr(8,6) + "/vt/frame"; 
     thmbElt.setAttribute("src", attrSubset); 
    }); 
</script> 
+0

谢谢,但它仍然没有工作... – F4125h4d

+0

显示的错误信息是什么?可以提供有关您的html和代码的更多细节。 –

+0

未捕获的TypeError:无法读取null的属性'substr' – F4125h4d

相关问题