IE td 元素的innerhtml 解决方法?

发布于 2024-09-15 12:48:37 字数 742 浏览 2 评论 0原文

我有以下功能:

<script type="text/javascript">
function changeText(elem){
  var oldHTML = document.getElementById(elem).innerHTML;
  var newHTML = "<span style='color:red'>" + oldHTML + "</span>";
  document.getElementById(elem).innerHTML = newHTML;
 }

</script>

和以下 HTML:

<table>
<tr>
<td id = "foo">bar</td>
</tr>
</table>

这会抛出一个“未知的运行时错误”(仅在 IE 中),谷歌搜索后告诉我,表元素在带有 innerHTML 的 IE 中是只读的。

我尝试寻找解决方法,但它们并没有针对我的具体问题,只是我想让“bar”这个词变成红色。我不想把“bar”这个词改成别的东西。纯粹是颜色变化。

有没有什么方法可以做到这一点,而不需要复杂而缓慢的 DOM 函数,并且无需更改页面上的任何其他 HTML 标记?这是一个包含表单的表,如果冲浪者提交的表单有错误,“错误”字段应该变成红色。它必须能够执行多次,因为冲浪者可能会弄乱多个字段。

感谢您的任何建议。

I have the following function:

<script type="text/javascript">
function changeText(elem){
  var oldHTML = document.getElementById(elem).innerHTML;
  var newHTML = "<span style='color:red'>" + oldHTML + "</span>";
  document.getElementById(elem).innerHTML = newHTML;
 }

</script>

And the following HTML:

<table>
<tr>
<td id = "foo">bar</td>
</tr>
</table>

This throws a "unknown runtime error" (just in IE) which googling has since taught me that table elements are read-only in IE with innerHTML.

I've tried finding workarounds but they don't target my specific problem, which is just that I want to make the word "bar" red. I don't want to change the word "bar" to something else. Purely a color change.

Is there any way to do this without a complex and slow DOM function and without having to change any of the other HTML markup on the page? This is a table with a form in it, and if the surfer submits the form with an error, the "errored" fields should turn red. It has to be able to execute multiple times because it's possible for a surfer to mess up more than one field.

Thanks for any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪装你 2024-09-22 12:48:37

为什么不直接将单元格更改为 color:'red'

var td = document.getElementById(elem);
td.style.color = 'red';

Why not just change the cell to have color:'red' :

var td = document.getElementById(elem);
td.style.color = 'red';
莫言歌 2024-09-22 12:48:37

有几种方法可以解决这个问题。这里有一些快速的方法可以帮助您继续前进,或者直到其他人提供更干净/更好的方法为止。

我更喜欢以下内容: 在 css 中定义错误类:

.errorHighlight { color:red;}

然后对于 javascript:

function changeText(elem){ 
  document.getElementById(elem).className="errorHighlight"; 
} 
function changeTextBack(elem){ 
  document.getElementById(elem).className=""; 
} 

这假设您尚未设置任何类。如果这样做,您可能需要读取名称,并在错误类后附加一个空格。

同样,有很多方法可以解决这个问题,您需要选择最适合您的应用程序的一种。

Couple ways to go about it. Here are some quick ones to get you going -- or until someone else comes with a cleaner/better method.

I prefer the following: define an error class in css:

.errorHighlight { color:red;}

Then for the javascript:

function changeText(elem){ 
  document.getElementById(elem).className="errorHighlight"; 
} 
function changeTextBack(elem){ 
  document.getElementById(elem).className=""; 
} 

This assumes that you don't have any classes set already. If you do you may need to read the name, and append the error class with a space.

Again, there are a lot of ways to approach this and you want to pick the one best suited for your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文