簡體   English   中英

使用jQuery text()函數將HTML標簽添加到網頁

[英]Add HTML Tags to webpage using jQuery text() function

我目前正在編寫代碼,其中當用戶單擊按鈕時,它將使用JavaScript使用HTML <iframe>標簽顯示另一個網頁的框架。

以下是我用於顯示到網頁的代碼:

<html>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        $("p").text('<iframe src="webpage.html" width="450" height="475" style="overflow:hidden"></iframe>');
    </script>
    <p></p>
</html> 

結果:
<iframe src="webpage.html" width="450" height="475" style="overflow:hidden"></iframe>

可悲的是,它只是簡單地打印出文本而沒有實際獲取框架。 我如何輸出文本是否存在任何問題,或者我完全使用了錯誤的功能?

我嘗試過...刪除標簽周圍的引號('...')

導致...該網頁無用

使用html代替文字...

    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        $("p").html('<iframe src="webpage.html" width="450" height="475" style="overflow:hidden"></iframe>');
    </script>
    <p></p>
</html>

注意文本和html具有不同的功能:

    Text() : it replaces the text inside the html div or class specified but
                 but not the html

    html() : it replaces the specified dom of html div/class  ,or to be more specific
             the function called returns or set the content (returning an innerHtml),
             when you use to return content, it return the first matches,
             if not it overwrites the content of all matched elements.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM