简体   繁体   English

使用Javascript输出HTML不适用于无序列表

[英]Using Javascript to output HTML doesn't work with unordered list

I am using javascript to add HTML into a variable which is passed into a DIV: 我正在使用javascript将HTML添加到传递到DIV的变量中:

var FN = 'options';
var gn = 'Bob';
var LN = 'Simpson';
var LN2 = 'Jetson';
var LN3 = 'Flintstone';
var formResult = document.getElementById("resultFinal");
var theEnd == 'yes';

Answer = "<p class='yes'>The following " + FN + " are available to the user: <ul><li>" + gn + " " + LN + "</li><li>" + gn + " " + LN2 + "</li><li>" + gn + " " + LN3 + "</li></ul><br>The following " + FN + " are available to the user: <ul><li>" + gn + " " + LN + "</li><li>" + gn + " " + LN2 + "</li><li>" + gn + " " + LN3 + "</li></ul></p>";
document.querySelector("#resultFinal").innerHTML+=Answer;

if (theEnd == 'yes') {              
    if (formResult.style.display == "none") {
        formResult.style.display = "block";
    } else {
        formResult.style.display = "none";
    }                   
}

<div id="resultFinal" style="display:none"></div>

The variable theEnd is just a toggle switch that hides the DIV that contains the content of the variable Answer. 变量theEnd只是一个切换开关,可隐藏包含变量Answer内容的DIV。

The output of Answer is displaying incorrectly in the browser. Answer的输出在浏览器中显示不正确。

If I do this: 如果我这样做:

Answer = "<p class='yes'>The following " + FN + " are available to the user:<br>The following " + FN + " are available to the user:</p>";
document.querySelector("#resultFinal").innerHTML+=Answer;

It displays correctly as : 正确显示为:

<p class='yes'>The following options are available to the user:<br>The following options are available to the user:</p>

But using the intended code as seen at the top, I get this: 但是使用顶部所示的预期代码,我得到了:

<p class='yes'>The following options are available to the user:</p><ul><li>Bob Simpson</li><li>Bob Jetson</li><li>Bob Flintstone</li></ul><br>The following options are available to the user:<ul><li>Bob Simpson</li><li>Bob Jetson</li><li>Bob Flintstone</li></ul><p></p>

When it should be this: 什么时候应该这样:

<p class='yes'>The following options are available to the user:<ul><li>Bob Simpson</li><li>Bob Jetson</li><li>Bob Flintstone</li></ul><br>The following options are available to the user:<ul><li>Bob Simpson</li><li>Bob Jetson</li><li>Bob Flintstone</li></ul></p>

Why is this happening and how can I fix it? 为什么会发生这种情况,我该如何解决?

Well, The mentioned code is working absolutely fine for me. 好了,上面提到的代码对我来说绝对正常。

Instead of 代替

var theEnd == 'yes';

I changed like : 我改变了像:

var theEnd = 'yes';

Removed double = and used only 1 = 删除double =,仅使用1 =

Please try to use <div> instead of <p> , as browser auto closes and auto opens missing start or end tag for <p> 请尝试使用<div>代替<p> ,因为浏览器将自动关闭,并且自动打开<p>缺少的开始或结束标记

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

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