Html里面的脚本标签? [英] Html inside script tags?

查看:104
本文介绍了Html里面的脚本标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能将通常在body标签中找到的东西放在脚本中?

就连图像一样:

 < img src =urlalt =some_text> 

我可以在< script> < img src =urlalt =some_text> < / script>



我希望用户能够复制以下内容:

 < script type =text / javascriptsrc =script.js>< / script> 

它也包含html代码,甚至包含如下内容:

 < script type =text / javascriptsrc =script.js>< / script> 
< script type =text / htmlsrc =htmlstuff>< / script>


解决方案

如果您需要混合脚本和HTML高度不要建议),您可以将各个脚本块与该HTML混合使用。例如,

 < script> 
//一些JS代码在这里
< / script>

< img src =urlalt =some_text>
更多HTML转到这里
< script type =text / javascriptsrc =script.js>< / script>
多一些HTML到这里

< script>
//一些JS代码在这里
< / script>

但是,这是不好的做法。



<如果你想让你的用户抓住链接到em,并让它产生HTML,你的JS代码必须产生这个HTML程序 - 查找 document.createElement element.appendChild 等。

请注意,如果您可以使用jQuery,您的生活就会变成在手动创建DOM元素以及加载完整HTML的能力方面要容易得多,例如使用 .load()方法。


Is it possible to put things normally found in between the body tags in a script?

Like even an image:

<img src="url" alt="some_text">

Could I have that in <script> <img src="url" alt="some_text"> </script>

I want the user to be able to just copy something like :

<script type="text/javascript" src="script.js"></script>

And it would include html code too, or even something like:

<script type="text/javascript" src="script.js"></script>
<script type="text/html" src="htmlstuff"></script>

解决方案

If you need to mix scripts and HTML (which I highly don't recommend) you can mix individual script blocks with that HTML. E.g.

<script> 
   //some JS code goes here
</script>

<img src="url" alt="some_text">
some more HTML goes here
<script type="text/javascript" src="script.js"></script>
some more HTML goes here

<script> 
   //some JS code goes here
</script>

But again - this is bad practice.

If you want your user grab link just to JS and have it produce HTML - your JS code have to produce that HTML programmaticaly - look up document.createElement, element.appendChild etc.

Note that if you can use jQuery - your life becomes a lot easier in both manually creating DOM elements as well as ability to load complete HTML e.g. using .load() method.

这篇关于Html里面的脚本标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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