javascript HTML 对象标记“数据”属性不会随 jQuery 更改而更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22248670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML Object tag 'data' attribute wont update with jQuery change
提问by
As you can see from this JSFiddle: http://jsfiddle.net/cYxy7/1/
正如您从这个 JSFiddle 中看到的:http: //jsfiddle.net/cYxy7/1/
I am using jQuery to change the 'data' attribute of an object tag. However, even though I am able to change the data attribute in the source code, the actual data stream of the object doesn't change and the same video is played. Does anyone know a fix?
我正在使用 jQuery 更改对象标记的“数据”属性。但是,即使我能够更改源代码中的数据属性,对象的实际数据流也不会更改,并且会播放相同的视频。有谁知道修复?
I have tried a few 'refresh' techniques and none have worked so far.
我尝试了一些“刷新”技术,但到目前为止都没有奏效。
HTML:
HTML:
<object data="currentURL"></object>
jQuery:
jQuery:
$(document).ready(function(){
$('body').on('click', "button#switch", function() {
$( "object" ).attr("data", newURL);
});
})
采纳答案by Alex
EDIT:
编辑:
I found out something even stranger. Changing the params does not work until you change the box model of the object:
我发现了更奇怪的事情。在更改对象的框模型之前,更改参数不起作用:
$(document).ready(function(){
$('body').on('click', "button#switch", function() {
$( 'object param[name="flashvars"]' ).attr("value", "hostname=www.twitch.tv&channel=liquidwifi&auto_play=true&start_volume=25");
$( "object").attr('data', "http://www.twitch.tv/widgets/live_embed_player.swf?channel=liquidwifi").hide().show();
});
})
this perfectly works: http://jsfiddle.net/W7NTd/1/
这非常有效:http: //jsfiddle.net/W7NTd/1/
For some strange reason changing the param values didnt work, only creating new html seems to work for me:
由于某些奇怪的原因,更改参数值不起作用,只有创建新的 html 似乎对我有用:
$(document).ready(function(){
$('body').on('click', "button#switch", function() {
$('object param[name="flashvars"]').attr('value', $('object param[name="flashvars"]').attr('value').replace('ivan', 'liquidwifi'));
var new_html = $('#wrapper').html();
$('#wrapper').html(new_html);
});
})
I added a wrapper for that.
我为此添加了一个包装器。
回答by Gertjan
The following code works for me
以下代码对我有用
$( "object" ).replaceWith('<object data="' + newURL + '"></object>');
回答by Denis Ali
The flash object is quite trickie to reload.
重新加载 flash 对象非常棘手。
Try using this simple flashloaderor SWFObject.
尝试使用这个简单的flashloader或SWFObject。
回答by row1
You need to update a parameter and then the data attribute:
您需要更新参数,然后更新数据属性:
$( "object param[name=flashvars]" ).attr("value", "hostname=www.twitch.tv&channel=liquidwifi&auto_play=true&start_volume=25");
$( "object").css('display', 'none');
$( "object").attr('data', "http://www.twitch.tv/widgets/live_embed_player.swf?channel=liquidwifi").css('display', '');
回答by Mayank Tripathi
Append this in your code. this might help.
将其附加到您的代码中。这可能会有所帮助。
$("object param[name=flashvars]").attr("value", $("object").attr("data"));
回答by aelor
the reason is that you are not changing the param value
原因是您没有更改参数值
While changing the object attr change the param flashvars value also
在更改对象属性的同时更改参数 flashvars 值
demo here : http://jsfiddle.net/cYxy7/3/
演示在这里:http: //jsfiddle.net/cYxy7/3/
$(document).ready(function () {
$('body').on('click', "button#switch", function () {
alert("The object's 'data' attritube is currently: " + $("object").attr("data"));
$("object").attr("data", "http://www.twitch.tv/widgets/live_embed_player.swf?channel=liquidwifi");
$("param[name='flashvars']").attr("value", "hostname=www.twitch.tv&channel=liquidwifi&auto_play=true&start_volume=25");
alert("The object's 'data' attribute is now: " + $("object").attr("data"));
});
})
cheers!
干杯!