gpt4 book ai didi

JavaScript 附加元素文本不显示

转载 作者:行者123 更新时间:2023-11-28 05:49:29 29 4
gpt4 key购买 nike

我正在尝试为用户创建一个在线待办事项列表,我希望它能够正常运行,这样当您输入任务并单击“JotIT”按钮时,该任务就会被附加。

到目前为止,追加功能有效,但由于某种原因文本不可见,即使当我检查它时,文本显示在 HTML 中。

<script>
var welcome = 'Welcome To JotIT';

var jotItem = function()
{
//Create & Get the necessary elements
var item = document.createElement('input'),
space = document.createElement('br'),
form = document.getElementById("listing"),
frag = document.createDocumentFragment(),
textVal = document.getElementById("input-jot");

//Set Attributes to list item
item.setAttribute('type', 'checkbox');
item.setAttribute('name', 'jot-list');

//If there is no input in the textbox then create an alert popup
if(textVal.value === "")
alert("Please insert a value");
else {
item.innerHTML = textVal.value;
frag.appendChild(item);
frag.appendChild(space);
form.appendChild(frag);
textVal.value = "";
}
}
</script>

</head>
<body>
<h1 id="head">JotIT</h1><br>
<p> <script type="text/javascript">document.write(welcome);</script></p>
<input type="form" id= "input-jot">
<button class = 'btn-info' id="jot-down" onclick=jotItem();>JotIt!</button>
<!-- TODO: Add form submit tag instead of ul tag -->
<!-- TODO: Align the list items -->
<form id = "listing" method="get">
<input type="checkbox" name="me"> Start </input>

</form>

</body>

最佳答案

你必须插入一个标签。输入应该是自闭/空元素。特别是复选框类型的输入不会正确显示标签。为此,请改用标签元素。

    var welcome = 'Welcome To JotIT';

var jotItem = function() {
//Create & Get the necessary elements
var item = document.createElement('input'),
label = document.createElement('label'),
space = document.createElement('br'),
form = document.getElementById("listing"),
frag = document.createDocumentFragment(),
textVal = document.getElementById("input-jot");

//Set Attributes to list item
item.setAttribute('type', 'checkbox');
item.setAttribute('name', 'jot-list');

//If there is no input in the textbox then create an alert popup
if (textVal.value === "")
alert("Please insert a value");
else {
label.innerText = textVal.value;
frag.appendChild(label); // here goes the label
frag.appendChild(item);
frag.appendChild(space);
form.appendChild(frag);
textVal.value = "";
}
}
</head>

<body>
<h1 id="head">JotIT</h1>
<br>

<input type="form" id="input-jot">
<button class='btn-info' id="jot-down" onclick=jotItem();>JotIt!</button>
<!-- TODO: Add form submit tag instead of ul tag -->
<!-- TODO: Align the list items -->
<form id="listing" method="get">
<label>Start</label>
<input type="checkbox" name="me" />

</form>

</body>

关于JavaScript 附加元素文本不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37421621/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com