将php输出读入js字符串?
基本上,我有一个 html 页面,我想创建一个单独的 javascript 文件,将标签之间的所有标记读取到 javascript 字符串中。有办法做到这一点吗?
编辑
好的,所以,详细说明一下...
在我的 tpl.php 文件中,我基本上有这些行 js 片段:
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript">var text = '<div id="open">test text</div>';</script>
<script type="text/javascript" src="/js/mypage.js"></script>
然后,在 mypage.js 中,我有这个:
$(document).ready(function() {
var content = text;
alert(content);
});
这在警报窗口中准确地显示“测试文本”,但我正在寻找实际上看起来像 var 文本的变量,以便它显示:
var content = '<div id="open">test text</div>';
Basically, I have an html page and I want to create a separate javascript file that reads all of the markup between the tags into a javascript string. Is there a way to do that?
EDIT
Okay, so, to elaborate a little more...
In my tpl.php file, I basically have these lines js snippet:
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript">var text = '<div id="open">test text</div>';</script>
<script type="text/javascript" src="/js/mypage.js"></script>
Then, in mypage.js, I have this:
$(document).ready(function() {
var content = text;
alert(content);
});
And this accurately displays "test text" in an alert window, but I'm looking for the variable to actually look like var text, so that it reads:
var content = '<div id="open">test text</div>';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您想用 JavaScript 读取 HTML 标签 (
...
) 的内容吗?容易做到:
编辑:修复语法错误。
EDIT2:使用
body
而不是html
标签。If I understand you correctly, you want to read the content of the HTML tag (
<html>...</html>
) with JavaScript?Easy to do:
EDIT: Fixed syntax error.
EDIT2: Used
body
instead ofhtml
tag.