这适用于IE9以外的所有浏览器,IE9上的getAttribute返回null,任何想法
function getFlashMovieObject(movieName){
if(document.embeds[movieName])
return document.embeds[movieName];
if(window.document[movieName])
return window.document[movieName];
if(window[movieName])
return window[movieName];
if(document[movieName])
return document[movieName];
}
function ChangePlaylist(newval,mymovievarmp3){
var mp3Player = getFlashMovieObject(mymovievarmp3);
var newstring = mp3Player.getAttribute('data');
var broken = new Array();
broken = newstring.split('?');
mp3Player.setAttribute('data',broken[0]+'?'+broken[1]+'?'+newval)
}
使用JQuery但仍无法正常工作
var mp3Player = getFlashMovieObject(mymovievarmp3);
var newstring = $("#mp3Player ").data("data");
答案 0 :(得分:0)
尝试
function ChangePlaylist(newval,mymovievarmp3){
var mp3Player = getFlashMovieObject(mymovievarmp3);
var newstring = mp3Player.getAttribute('data');
var broken = new Array();
broken = newstring.split('?');
mp3Player.setAttribute('data',broken[0]+'?'+broken[1]+'?'+newval)
}
我现在明白为什么你逃脱了。我认为它打破了getAttribute()行 我建议导入jQuery并使用$(elem).data()。
说你有:
<a data-someval="blah" />
然后这个:
$("#elem").data("someval");
应该回报。
答案 1 :(得分:0)
请尝试以下代码:
function ChangePlaylist(newval,mymovievarmp3){
var mp3Player = getFlashMovieObject(mymovievarmp3);
//previus declaration
//var newstring = mp3Player.attributes['data'].nodeValue;
//Although i'm not familiar with getFlashMoiveObject, if that function
//a reference to the javascript object, the next should work fine.
var newstring = $(mp3Player).attr('data');
//If that does not work, then try adding an `id`, or a `class` to the object
//and then do one of the next:
//using `id`:
//var newstring = $('#mp3PlayerId').attr('data');
//using `class`:
//var newstring = $('.mp3PlayerClass').attr('data');
//Also note that `id` and `class` are standard attributes, and so they values can
//be accessed using regular getAttribute.
var broken = new Array();
broken = newstring.split('?');
//mp3Player.setAttribute('data',broken[0]+'?'+broken[1]+'?'+newval)
//Update - To set the a value to data attribute:
$(mp3Player).attr('data',broken[0]+'?'+broken[1]+'?'+newval);
}
IE在从元素中获取非标准属性时遇到问题。
<强>更新强>:
如果您愿意使用其他库,例如jQuery
,您可以轻松地执行类似
$('#elementId').attr('data');
肯定会成为crossbrowser。
更新2
我更新了代码并解释了一下。希望它可以帮助你!
更新3
以下代码可用于设置数据值:
$(mp3Player).attr('data',value);