js改變embed標(biāo)簽src值的方法

字號:


    本文實例講述了js改變embed標(biāo)簽src值的方法。分享給大家供大家參考。具體分析如下:
    今天有一需求,一堆視頻,一堆鏈接,點擊相關(guān)的鏈接,在本頁打開相關(guān)的視頻。
    第一想法,很簡單么,直接把src值改成點擊的那個的href值就可以了。
    試了下,發(fā)現(xiàn)這樣不行,視頻該怎么放還是怎么放,永遠(yuǎn)是剛打開那個。
    第二想法,給embed外面加個標(biāo)簽,把里面內(nèi)容清空,再寫進(jìn)去,這樣總可以了吧。
    試了下,和上面一樣,依舊不行。
    又試了許多類似方法,還是不行。
    最后,想的,把embed標(biāo)簽隱藏(display:none),再清空試下。終于可以了!
    代碼如下:
    var tabv = document.getelementbyid(f_tabv);
    var tabva = tabv.getelementsbytagname(a);
    var tabcv = document.getelementbyid(f_tab_cv);
    tabcv.innerhtml = '<embed src=abc.wmv autostart=true
    width=545 height=325 type=video/x-ms-asf></embed>';
    for(var i=0; i<tabva.length; i++){
    tabva[i].onclick=function(){
    var href1 = this.getattribute(href);
    var href2 = '<embed src='+href1+' autostart=true
    width=545 height=325 type=video/x-ms-asf></embed>';
    tabcv.getelementsbytagname(embed)[0].style.display=none;
    tabcv.innerhtml=;
    tabcv.innerhtml=href2;
    for(i=0; i<tabva.length; i++){
    tabva[i].classname='';
    }
    this.classname = act;
    return false;
    }
    }