是否可以在JavaScript中包含HTML代码? [英] Is it possible to include HTML code in JavaScript?

查看:55
本文介绍了是否可以在JavaScript中包含HTML代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本问题,我可以在JS中包含HTML代码吗? (使用document.write)



这是我的HTML代码:

 < li>< a href =#class =menulink> text< / a>< / li> 
< li>< a href =#class =menulink> text< / a> < /锂>
< li>< a href =#class =menulink> text< / a>
< ul>

< li>
< a href =#class =sub> text< / a>
< ul>
< li class =topline>< a href =#> text< / a>< / li>
< li><#> text< / a>< / li>
< li>< a href =#> text< / a>< / li>
< li>< a href =#> text< / a>< / li>
< li>< a href =#> text< ; / a>< / li>
< li>< a href =#text< / a>< / li>
< li>< a href =# > text< / a>< / li>

< / ul>
< / li>
< li>
< a href =#class =sub> text< / a>
< ul>
< li class =topline>< a href =#> text< / a>< / li>
< li>< a href =#> text< / a>< / li>



< / ul>
< / li>

< li>
< a href =#class =sub> text < / a>< / li>
< li>
& lt; a href =#class =sub>文字< / a>< / li>
< / ul>
< / li>

< li>
< a href =#class =menulink> text< / a>


< / li>
< li>< a href =#class =menulink> text< / a>

我希望将其包含在此JS代码中

  window.onload = function(){
document.getElementById(menu)。innerHTML =;

通过以下代码连接:

 < p id =dropdown_menu>< / p> 

我该怎么办?



完整代码在这里 http://jsfiddle.net/tsnave/eSgWj / 4 /
谢谢..

解决方案

另一种方法是放HTML在脚本标记内:

 < script type =text / templateid =myHtml> 
< li class =topline>< a href =#>一些文字< / a>< / li>
< li>< a href =#>一些文字< / a>< / li>
< li>< a href =#>一些文字< / a>< / li>
< li>< a href =#>一些文字< / a>< / li>
< li>< a href =#some text< / a>< / li>
< li>< a href =#>一些文字< / a>< / li>
< li>< a href =#>一些文字< / a>< / li>
< / script>

然后你可以使用



<进入Javascript pre> var myHtml = document.getElementById('myHtml')。innerHTML;

或使用多个库中的一个来帮助您解决此问题。浏览器不会解释或显示带有 type =text / template的脚本标记内的代码。这种方法的优势在于它可以直接在Javascript中将其直接放入字符串中,它允许您在编辑器中将其视为普通HTML,并保持Javascript清洁。另请参阅 John Resig的这篇文章


I have a basic question, can i include HTML code in JS? (with "document.write")

This is my HTML code:

    <li><a href="#" class="menulink">text</a></li>
<li><a href="#" class="menulink">text</a> </li>
<li><a href="#" class="menulink">text</a>
    <ul>

        <li>
            <a href="#" class="sub">text</a>
            <ul >
                <li class="topline"><a href="#">text</a></li>
                <li><#">text </a></li>
                <li><a href="#">text</a></li>
                <li><a href="#">text</a></li>
                <li><a href="#">text</a></li>
                <li><a href="#"text</a></li>
                <li><a href="#">text</a></li>

            </ul>
        </li>
        <li>
            <a href="#" class="sub">text </a>
            <ul>
                <li class="topline"><a href="#">text</a></li>
                <li><a href="#">text</a></li>



    </ul>
</li>

    <li>
                    <a href="#" class="sub"> text  </a></li>
            <li>
                    <a href="#" class="sub"> text  </a></li>
           </ul>
        </li>

            <li>
    <a href="#" class="menulink">text</a>


</li>
<li><a href="#" class="menulink">text</a>

and i want to include it in this JS code

window.onload = function () {
    document.getElementById("menu").innerHTML="";

connect it by this code:

<p id="dropdown_menu"></p>

how can i do it?

the full code is here http://jsfiddle.net/tsnave/eSgWj/4/ thanks..

解决方案

A different way of doing it is to put the HTML inside a script tag:

<script type="text/template" id="myHtml">
    <li class="topline"><a href="#">some text</a></li>
    <li><a href="#">some text </a></li>
    <li><a href="#">some text</a></li>
    <li><a href="#">some text</a></li>
    <li><a href="#"some text</a></li>
    <li><a href="#">some text</a></li>
    <li><a href="#">some text</a></li>
</script>

Then you can get it into Javascript using

var myHtml = document.getElementById('myHtml').innerHTML;

or using one of several libraries which help you with this. Code inside a script tag with type="text/template" will not be interpreted or shown by the browser. The advantage of this approach over putting it straight into a string in Javascript is that it allows you to keep treating it as normal HTML in your editor, and it keeps the Javascript clean. See also this post by John Resig.

这篇关于是否可以在JavaScript中包含HTML代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆