简体   繁体   English

innerHTML替换不反映

[英]innerHTML replace does not reflect

I have HTML something like this 我有像这样的HTML

<div id="foo">
<select>
<option> one </option>
</select>
</div>

when I use 我用的时候

document.getElementById('foo').innerHTML = document.getElementById('foo').innerHTML.replace("<option> one </option>", "<option> two </option>") ;

The innerHTML get replaced but it does not get reflected in the browser. innerHTML被替换但它没有在浏览器中反映出来。

If I alert innerHTML I can see that it has been changed now but in the browser it still shows old option. 如果我提醒innerHTML,我可以看到它现在已被更改,但在浏览器中它仍显示旧选项。

Is there any way to make this change recognized by the browser ? 有没有办法让浏览器识别出这种变化?

Thanks in advance. 提前致谢。

works fine for me in Firefox 3.5 在Firefox 3.5中适用于我

Working Demo 工作演示

EDIT: You must be using IE. 编辑:你必须使用IE浏览器。 You need to create a new option and add it to the element, or amend the text of the existing option 您需要创建一个新选项并将其添加到元素,或修改现有选项的文本

Working Demo - tested and working in FireFox and IE 7. 工作演示 - 在FireFox和IE 7中测试和工作。

var foo = document.getElementById('foo');
var select = foo.getElementsByTagName('select');
select[0].options[0].text = ' two ';

Better to give an id to the select tag and use new option to add an item. 最好为select标记赋予id并使用new选项添加项目。

var sel =document.getElementById ( "selectElementID" );
var elOptNew = document.createElement('option');
elOptNew.text = 'Insert' + 2;
elOptNew.value = 'insert' + 2;
sel.options.add ( elOptNew );

If you need to replace an option 如果您需要更换选项

sel.options[indexNumber].text = 'text_to_assign';
sel.options[indexNumber].value = 'value_to_assign';

Setting the innerHTML of a select has a bug in IE. 设置select的innerHTML在IE中有一个错误。 Not sure if they fixed this in 7 or 8 yet. 不确定他们是否已将其固定为7或8。 It cuts off some of the text you give it and thus has malformed options. 它会切断您提供的一些文本,因此选项格式不正确。 So you have to set the outerHTML which includes the select. 所以你必须设置包含select的outerHTML。 Since outerHTML is IE only (or was) I normally use select.parentNode.innerHTML. 由于outerHTML只是IE(或者是),我通常使用select.parentNode.innerHTML。 And I assign the ID to the select. 我将ID分配给选择。 But since you are setting the innerHTML of the div, you are already covered. 但是既然你正在设置div的innerHTML,那么你已经被覆盖了。 That was just an FYI. 那只是一个FYI。

I think your real problem is that replace is expecting a regular expression for the first argument. 我认为你真正的问题是替换期望第一个参数的正则表达式。 Or that the option element is not what you expect. 或者选项元素不是您所期望的。 IE can capitalize the option name and might be adding a selected attribute. IE可以大写选项名称,也可以添加选定的属性。 You should alert the innerHTML to see what it is before attempting to do a string replace. 在尝试执行字符串替换之前,您应该提醒innerHTML以查看它是什么。 Then I would use a valid regular expression to do the replace. 然后我会使用有效的正则表达式来进行替换。 You'll probably have to escape the slash with a backslash. 你可能不得不用反斜杠来逃避斜线。 I'm don't think you need to escape the angle brackets but I'm not 100% on that. 我不认为你需要逃避尖括号,但我不是100%。

Also in my experience the DOM method (new Option) is slower. 根据我的经验,DOM方法(新选项)速度较慢。 When you have to replace a large number of options it is significantly slower. 当你必须更换大量选项时,它会明显变慢。 If you just have to replace one or two, you can use the DOM method. 如果您只需要替换一个或两个,则可以使用DOM方法。

I guess you are using IE? 我猜你在使用IE浏览器? The problem is that you have to use new Option(...) to populate SELECTs in IE. 问题是您必须使用新的选项(...)来填充IE中的SELECT。 Like this: 像这样:

document.getElementsByTagName('select')[0].options[0]=new Option(' two ');

Edit: There is a Microsoft Knowledgebase article about this bug: http://support.microsoft.com/kb/276228 编辑:有关于此错误的Microsoft知识库文章: http//support.microsoft.com/kb/276228

A solution I worked out goes as follows 我制定的解决方案如下

strHTML = "&nbsp;<option value=""1"">Text1</option>"
strHTML = strHTML + "<option value=""15"">Text2</option>"
strHTML = strHTML + "<option value=""17"">Text3</option>"


document.getElementById("id-selectbox").innerHTML=strHTML;
if(Browser=="Internet Explorer"){
    document.getElementById("id-selectbox").outerHTML=document.getElementById("id-selectbox").outerHTML;
}

The HTML string could be obtained by an AJAX request. HTML字符串可以通过AJAX请求获得。

The result is that the browser will rewrite/render the selectbox with its newly added options. 结果是浏览器将使用新添加的选项重写/渲染选择框。

Notice the: &nbsp preciding the first option code If you leave this out, the first option will loose its option tags and therefor will not be shown in the new added select-innerHTML 注意: &nbsp预先设置第一个选项代码如果你把它排除在外,第一个选项会松开它的选项标签,因此不会在新添加的select-innerHTML中显示

You're using replace which requires as the first argument a RegEx. 您使用的是需要作为RegEx的第一个参数的replace

Try this: 试试这个:

document.getElementById('foo').innerHTML = document.getElementById('foo').innerHTML.replace(/<option> one <\/option>/, "<option> two </option>");

Does this work for you? 这对你有用吗?

Check this 检查一下

document.getElementById('foo').innerHTML.replace("<option> one </option>", "<option> two </option>") ;

if it is not working change the browser. 如果它不工作改变浏览器。

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

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