简体   繁体   English

使用元素的值与innerHTML?

[英]Using value of an element vs innerHTML?

I know they say innerHTML is not safe and can be slow, even though it is now part of HTML5. 我知道他们说innerHTML不安全,而且运行缓慢,即使它现在已成为HTML5的一部分。 On the other hand using DOM can be verbose. 另一方面,使用DOM可能很冗长。

What about using a value of eg value of a button? 使用例如按钮值的值怎么办?

function changeit (valueff, idP){
var change_item = prompt("Change this item",valueff);    
if (change_item)
{
    document.getElementById(idP).value = change_item;
}
...

It's a restaurant menu made with Codeigniter. 这是用Codeigniter制作的餐厅菜单。 So function receives value of a button, and its id and then the value is changed according to admin's input. 因此,函数接收按钮的值及其ID,然后根据管理员的输入来更改该值。 Then it makes an ajax call to a database and changes the relevant entry. 然后,它对数据库进行ajax调用并更改相关条目。 Im planning to sanitize the input of course. 我打算对输入的内容进行消毒。

So is the above better than using a textarea and innerHTML?: 那么上述内容比使用textarea和innerHTML更好吗?

 document.getElementById(idP).innerHTML = change_item;

The value to be changed could be a line in the menu eg Espresso $2.50, it doesnt matter if I use a button as long as the admin can click on it and change the value in the actual menu. 要更改的值可以是菜单中的一行,例如Espresso $ 2.50,只要管理员可以单击它并更改实际菜单中的值,我是否使用按钮都没关系。

<input id="list_5" type="button" value="Espresso $2.50"   onclick="changeit(this.value,this.id);">

EDIT: 编辑:

If you google why is innerHTML bad - you will get a lot of results, for (example this answer on SO)[ Why is "element.innerHTML+=" bad code? 如果您用谷歌搜索为什么innerHTML不好-您会得到很多结果,例如(例如SO上的这个答案)[ 为什么“ element.innerHTML + =”是不好的代码?

when you submit a form it sends its inputs value not innerHTML. 提交表单时,它发送的输入值不是innerHTML。

but textarea has special behavior that when you change innerHTML it automaticlly change value . 但是textarea具有特殊的行为,当您更改innerHTML它会自动更改value

so for example if you have textarea: 因此,例如,如果您有textarea:

<textarea id='test'></textarea>

and you set its innerHTML: 然后设置其innerHTML:

var textarea=document.getElementById('test');
textarea.innerHTML='this text is in innerHTML';
conosole.log(textarea.value); //'this text is in innerHTML'

if you tried to set innerHTML for input or button its value won't be changed like textarea so when you send the form it will send empty string. 如果您尝试为输入或按钮设置innerHTML,其值将不会像textarea一样被更改,因此,当您发送表单时,它将发送空字符串。

<input type='text' id='inp' />

var inp=document.getElementById('inp');
inp.innerHTML='this text is in innerHTML';
conosole.log(inp.value); //'' (empty string)

You're mixing cows with ballet. 你把牛和芭蕾舞混在一起。
If you only need to operate over input s and textarea s, let's make it short: use 如果只需要对input s和textarea进行操作,我们就简短一点:
yourElement.value = "some value" . yourElement.value = "some value"

(You cannot operate using innerHTML over an input element in any case.) (在任何情况下,您都不能在input元素上使用innerHTML进行操作。)

Try to use textContent instead of innerHTML . 尝试使用textContent而不是innerHTML

Using innerHTML to modify a document causes the document to be re-parsed, which is inefficient and has critical drawbacks, including invalidating any JavaScript reference to replaced DOM nodes, clearing any JavaScript properties and event listeners on replaced DOM nodes, and re-executing any script tags in the changed markup. 使用innerHTML修改文档会导致文档被重新解析,这效率低下并且具有严重的缺陷,包括使对替换后的DOM节点的所有JavaScript引用无效,清除替换后的DOM节点上的所有JavaScript属性和事件监听器,以及重新执行更改后的标记中的脚本标签。

Hope it will be useful for you. 希望对您有用。

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

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