2017-08-27 59 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

相關問題