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