第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript 創(chuàng)建 iframe 設(shè)置內(nèi)容然后從一個函數(shù)返回它

Javascript 創(chuàng)建 iframe 設(shè)置內(nèi)容然后從一個函數(shù)返回它

函數(shù)式編程 2021-12-12 15:49:00
我需要動態(tài)創(chuàng)建一個 iframe,設(shè)置它的 html,然后將它作為一個函數(shù)返回,以便以后可以使用 newAdUnit() 調(diào)用它?,F(xiàn)在它返回 [object HTMLIFrameElement]。我試圖找出一種方法來從一個函數(shù)中完成這一切。這樣做的原因是我正在設(shè)置需要動態(tài)加載的廣告。單個函數(shù)會使我的代碼更簡潔,因為我可以用多種不同的方式調(diào)用它。有任何想法嗎?<script>function newAdUnit(size) {    var iframe = document.createElement('iframe');    iframe.onload = function() {        iframe = iframe.contentWindow.document;        var html = '<body>This is a test</body>';        iframe.open();        iframe.write(html);        iframe.close();    };    return iframe;}</script><div id="test"><script>document.getElementById("test").innerHTML = newAdUnit()</script></div>
查看完整描述

2 回答

?
慕雪6442864

TA貢獻1812條經(jīng)驗 獲得超5個贊

您應(yīng)該只.innerHTML在要添加的內(nèi)容是 HTML 字符串時使用。但是,在您的情況下,您有一個HTMLIFrameElement對象,因此.innerHTML在這種情況下不能使用。目前,Javascript 正在隱式調(diào)用.toString()由 返回的元素對象newAdUnit(),這會導(dǎo)致[object HTMLIFrameElement].


相反,當你想將一個節(jié)點元素添加到另一個元素時,你可以.appendChild()像這樣使用:


<div id="test"></div>


<script>

  function newAdUnit(size) {

    var iframe = document.createElement('iframe');

    iframe.onload = function() {

      iframe = iframe.contentWindow.document;

      var html = '<body>This is a test. The size is ' + size + '</body>';

      iframe.open();

      iframe.write(html);

      iframe.close();

    };

    return iframe;

  }


  document.getElementById("test").appendChild(newAdUnit(10));

</script>


查看完整回答
反對 回復(fù) 2021-12-12
?
慕后森

TA貢獻1802條經(jīng)驗 獲得超5個贊

如果您在當前操作中使用JQuery,將會非常容易。下面的代碼顯示了如何


<html>

  <head>

  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <script>

  $(document).ready(function() {

    function newAdUnit(obj) {

        var iframe = document.createElement('iframe');

        iframe.onload = function() {

            iframe = iframe.contentWindow.document;

            var html = '<body>This is a test</body>';

            iframe.open();

            iframe.write(html);

            iframe.close();

        };

        $(obj).append(iframe);

    }


    newAdUnit($('#test'));

  });

  </script>

  </head>

  <body>

    <div id="test">


    </div>

  </body>

</html>


查看完整回答
反對 回復(fù) 2021-12-12
  • 2 回答
  • 0 關(guān)注
  • 309 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號