[英]Linking javascript error with hard-coded html in C# string builder object
This is probably the first time I had to deal with a javascript error when studying somebody else's code. 这可能是我在研究别人的代码时第一次必须处理javascript错误。 Please help me out.
请帮帮我。
This webform is supposed to send an email when one submits a buttton. 当有人提交按钮时,该网络表单应该发送电子邮件。 However, a javascript error fires up preventing it from doing so:
但是,会触发javascript错误,阻止这样做:
So this is where it takes me to when i hit Yes 所以这是我击中是要带我去的地方
I tracked down and see this piece of code below 我跟踪并在下面看到了这段代码
Question is: Does property "display" in the popup refer to the value of attribute 'style' of the row tag < td >? 问题是:弹出窗口中的“显示”属性是否引用行标签<td>的属性“样式”的值? cellColor is initialized to 'ffffff', ie cellColor = "ffffff";
cellColor初始化为'ffffff',即cellColor =“ ffffff”; would cellColor cause the property 'display' undefined/null if cellColor is ever be undefined/null?
如果cellColor曾经是undefined / null,cellColor是否会导致属性“ display”为undefined / null?
align='left valign=middle style=background-color: #" + cellColor + "; align ='left valign = middle style = background-color:#“ + cellColor +”; border: solid 1px #000000;\\">"
边框:实心1px#000000; \\“>”
(This is the codes I tracked down) (这是我跟踪的代码)
I'm an admitted JS hack; 我是公认的JS骇客; a JS guru may have a better solution...
JS大师可能有更好的解决方案...
The issue here is that the divHRSupervisors.childNodes[i].style.display != 'none'
will throw the undefined error whenever a child node does not contain an inline style with a display attribute, ie, 这里的问题是,每当子节点不包含带有显示属性的内联样式时,
divHRSupervisors.childNodes[i].style.display != 'none'
都会引发未定义的错误。
Will work: 将工作:
<div style="display: block;">content</div>
- or -
<div style="display: none;">content</div>
Will not work: 不管用:
<div>content</div>
Additionally, whenever childNodes
is used, all nodes are included, including text. 此外,无论何时使用
childNodes
,都将包括所有节点,包括文本。 For example... 例如...
0 child nodes for the 'Test' div: 'Test'div的0个子节点:
<div id="Test"></div>
1 child nodes for the 'Test' div (the text inside the div counts as a child node): 'Test'div的1个子节点(div内的文本算作一个子节点):
<div id="Test">content</div>
3 child nodes for the 'Test' div (1 for the main text content, 1 for the inner <div>
and 1 for the inner <div>
's content): 3个用于“测试” div的子节点(1个用于主要文本内容,1个用于内部
<div>
,1个用于内部<div>
的内容):
<div id="Test">
content
<div>child content</div>
</div>
With the conditional test in your current JS, anytime a child node is encountered that does not have the inline style of display set, an error will be thrown. 使用当前JS中的条件测试,只要遇到子节点没有内联样式的显示集,就会抛出错误。
One solution would be to add a common class to any elements you want to check for the style.display
property and then check to see if the display is set to none. 一种解决方案是将公共类添加到要检查
style.display
属性的任何元素,然后检查显示是否设置为none。 Something along the lines of: 类似于以下内容:
var divs = document.getElementById("divHRSupervisors");
var children = divs.childNodes;
for (var i = 0; i < children.length; i++) {
var child = children[i];
// someClass is a class applied to all elements you want to verify
if (child.className == 'someClass') {
// check to see if there is a display property and if the display
// property does not equal none.
if (child.style.display && child.style.display != 'none') {
alert(child.nodeName);
// do other stuff.
}
}
}
Your HTML will look something like the following (note the use of the someClass): 您的HTML将类似于以下内容(请注意someClass的使用):
<div id="divHRSupervisors">
<div class="someClass">hello</div>
<div class="someClass" style="display: block;">world</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.