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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

函數(shù)式編程 2021-12-12 15:49:00
我需要?jiǎng)討B(tài)創(chuàng)建一個(gè) iframe,設(shè)置它的 html,然后將它作為一個(gè)函數(shù)返回,以便以后可以使用 newAdUnit() 調(diào)用它。現(xiàn)在它返回 [object HTMLIFrameElement]。我試圖找出一種方法來(lái)從一個(gè)函數(shù)中完成這一切。這樣做的原因是我正在設(shè)置需要?jiǎng)討B(tài)加載的廣告。單個(gè)函數(shù)會(huì)使我的代碼更簡(jiǎn)潔,因?yàn)槲铱梢杂枚喾N不同的方式調(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貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

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


相反,當(dāng)你想將一個(gè)節(jié)點(diǎn)元素添加到另一個(gè)元素時(shí),你可以.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>


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

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

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


<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>


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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