javascript
  • jquery
  • html
  • iframe
  • 2013-11-26 27 views 0 likes 
    0

    我正在通過jquery向我的html附加iframe。代碼是這樣的。通過jquery失敗將src添加到iframe

    function setImageUrl(imgname) { 
    
        $(".large-image iframe").remove(); 
        var u = '<iframe width="560" id="iframevideo" height="315" src="#" frameborder="0" allowfullscreen></iframe>'; 
        $(".large-image").append(u); 
        document.getElementById('iframevideo').setAttribute("src", imgname); 
    return true; 
    

    } 參數imgname正確到來,這是預期Iframesrc 但這總是產生一個空白iframe。像這樣的

    enter image description here

    在另一方面,如果我硬編碼了src一切正常fine.So我認爲這個問題是有些地方我設置src。我已經嘗試了不同的methodes,但沒有任何工程。

    +0

    如果你打算使用jQuery,使用jQuery。沒有理由你應該使用'getElementById'和'setAttribute'。不一致性會讓你的代碼更難理解。 – m59

    回答

    1

    嘗試

    ​​

    $('#iframevideo').attr('src', imgname); 
    
    0

    有工作演示..

    function setimage() { 
        $("#oldframe").remove(); 
        $("#main").append("iframe tag "); 
        $('#newframe').attr('src', 'imgurl'); 
    } 
    

    Demo

    相關問題