简体   繁体   English

在C#字符串生成器对象中将javascript错误与硬编码的html链接

[英]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错误,阻止这样做:

javacript弹出窗口

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.

相关问题 通过硬编码的html select设置c#mvc会话变量 - set c# mvc session variable via hard-coded html select 在C#应用程序中访问硬编码数据 - Accessing hard-coded data in a C# application 来自web.config的连接字符串失败,其中相同的硬编码字符串成功(带有C#的ASP.NET) - connection string from web.config fails where identical hard-coded string succeeds (ASP.NET with C#) 在C#中使用目标配置时,不允许使用SAP硬编码登录参数 - SAP Hard-coded logon parameters not allowed when using a Destination Configuration in C# C#验证:没有硬编码的属性名字符串的IDataErrorInfo? - C# validation: IDataErrorInfo without hard-coded strings of property name? C#硬编码的SQL可以工作,但是使用SqlCommand参数却不能 - C# Hard-coded SQL works but using SqlCommand parameters doesn't 填充@ Html.DropDownListFor - 来自硬编码值的OK,来自数据库的错误 - Populating @Html.DropDownListFor — OK from hard-coded values, error from database 通用对象上的动态LINQ(没有任何硬编码属性) - Dynamic LINQ on a generic object (without any hard-coded properties) 使用对象名称作为与ActionLink的链接,而不是硬编码字面量 - Use Object Name as Link with ActionLink, Not Hard-coded Literal 将许多方法中使用的一个硬编码字符串重构为两个 - Refactoring one hard-coded string, used in numerous methods, into two
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM