如何将值添加到元素属性的末尾? - javascript

时间:2015-12-23 03:39:01

标签: javascript jquery html iframe youtube

我知道还有其他方法可以解决这个问题,但这只是突然出现在我脑海中,我无法找到答案。

我有物品'价值被环绕的物业'值。 我得到了这个值,并希望将其传递给元素的属性但是属性中已有值,我想要属性'来自对象的值是属性值的一部分。

让我们以youtube videoID为例

在javascript中,这里存储youtube的videoId

request.execute(function(response){
    var results = response.result;
    $.each(results.items, function(index, item){
        var videoId = item.id.videoId;
    });
});

在html中使用iframe

<iframe width="640" height="360" src="https://www.youtube.com/embed/VIDEOID" frameborder="0" allowfullscreen></iframe>

以上,我们可以看到来自js的videoId是否已插入html中的VIDEOID,然后视频将显示在html页面上。

如何使用普通的javascript / jQuery将videoId值传递到VIDEOID

2 个答案:

答案 0 :(得分:1)

为iframe设置ID,如此

<iframe width="640" height="360" src="" frameborder="0" id = "yt_iframe" allowfullscreen></iframe>

并将其添加到您的jQuery

$("#yt_iframe").attr("src", "http://youtube.com/embed/" + videoId)
我没有尝试过,但应该这样做。

答案 1 :(得分:0)

假设我有一个名为VideoID的属性的iframe,其中包含一些值。

现在我想将VideoID的值附加到src,我会使用下面的方法。

//Looping through all the iframes    
$("iframe").each(function(){

   //appending the value if video ID to the SRC    
   $(this).attr('src',$(this).attr('src')+$(this).attr('VideoID'));

});

希望这会有所帮助。

相关问题