简体   繁体   English

设置给定标签的html内容

[英]Setting the html content of a given tag

I'm trying to generate the content of an html page using javascript. 我正在尝试使用javascript生成html页面的内容。 The contents of the tag I need to replace are the ones in the second : 我需要替换的标签内容是第二个内容:

<script id="accountview" type="text/view">
    <!-- We show the menu bar regardless of the window we are viewing as long as we are logged in -->
    <div class="panel">
      <button onclick="go_home()">Home</button>
      <button onclick="go_browse()">Browse</button>
      <button onclick="go_account()">Account</button>
    </div>
    <div id="welcome" class="welcome">
    <!-- Content to be replaced -->
    </div>
</script>

I'm using the following function to generate the content: 我正在使用以下功能来生成内容:

function go_account()
{
    // Get the currently logged in user's data(name, surname, e-mail, country, city)
    var userData = serverstub.getUserDataByToken(localStorage.getItem("current_user")).data;
    // To get the actual user attributes use ".": userData.firstname
    var userInfo = "<div class=\"welcome\"> User Information: <br>";
    userInfo += "email:" + userData.email + "<br>";
    userInfo += "firstname:" + userData.firstname + "<br>";
    userInfo += "lastname:" + userData.lastname + "<br>";
    userInfo += "gender:" + userData.gender + "<br>";
    userInfo += "city:" + userData.city + "<br>";
    userInfo += "country:" + userData.country + "<br>";
    userInfo += "</div>";
    // Change the <div> with the user info
    var element = document.getElementById("welcome");
    element.innerHTML = userInfo;
}

but the returned element is always null. 但是返回的元素始终为null。 Why? 为什么?

Not sure if I understand you correctly, but is it simply that you're missing a return element; 不知道我是否正确理解您的意思,仅仅是因为您缺少一个 return element; at the end of your function? 在您的功能结束时? In JavaScript, you must return explicitly, unlike some other languages. 在JavaScript中,您必须显式返回,这与某些其他语言不同。

Edited to say: 编辑说:

I re-read your question and the issue is indeed (like Jacque explained in another answer) that the contents of your script tag aren't seen as HTML. 我重新阅读了您的问题,问题确实是(就像Jacque在另一个答案中解释的那样),您的脚本标签的内容未视为HTML。

See this JSFiddle for a smaller example of the problem: https://jsfiddle.net/Lxyamptk/ 请参阅此JSFiddle以获取问题的较小示例: https ://jsfiddle.net/Lxyamptk/

And see this JSFiddle for a possible solution using jQuery: https://jsfiddle.net/Lkd180zk/ 并查看此JSFiddle以获取使用jQuery的可能解决方案: https ://jsfiddle.net/Lkd180zk/

HTML: HTML:

<script id="accountview" type="text/view">
  <div>
    <div id="welcome" class="welcome"></div>
  </div>
</script>

<div id="output">
    output goes here
</div>

JS with jQuery: 带有jQuery的JS:

var templateAsText = $("#accountview").html();
var templateAsHTML = $(templateAsText);
templateAsHTML.find("#welcome").text("Welcome!");
$("#output").html(templateAsHTML);

Note that for $(templateAsText) to work, the template must have a HTML element as its root (like the wrapper div in my example). 请注意,要使$(templateAsText)起作用,模板必须以HTML元素作为其根(如本例中的wrapper div)。 You'll get an error from templateAsText = "hello <div></div> hello" , for example. 例如,您会从templateAsText = "hello <div></div> hello"得到错误。

Any content written inside a script tag is considered as CDATA. 脚本标记内写入的任何内容均视为CDATA。 In W3 lingo, this means it is not parsed as HTML, but as raw text. 在W3语言中,这意味着它不会被解析为HTML,而是被解析为原始文本。 Script tags however have a default style of display: none; 但是,脚本标记具有默认的display: none;样式display: none;默认display: none;样式display: none; which is why you don't see their content on the page. 这就是为什么您在页面上看不到他们的内容的原因。

https://stackoverflow.com/a/5679857/2813263 https://stackoverflow.com/a/5679857/2813263

Long story short: don't put HTML in script tags. 长话短说:不要将HTML放在脚本标签中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM