dojoquery与最近的方法无法在Internet Explorer中工作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dojoquery与最近的方法无法在Internet Explorer中工作相关的知识,希望对你有一定的参考价值。

我试图在dojo中使用最接近的方法来找出我想要显示/隐藏的元素的第一个父元素,这是基于在过滤选择之一中选择的值。它在firefox浏览器中工作正常,但在IE中它说Object不支持Closest。这里的任何帮助都非常感谢。下面给出了html和js代码的示例。

HTML

<div class="pvrProperty pvrLayoutItem pvrPropertyRequired" id="pvr_widget_Property_3" widgetId="pvr_widget_Property_3">
<label class="pvrPropertyLabel" id="pvr_widget_Property_3_label" for="pvr_widget_editors_FilteringSelectEditor_0" data-dojo-attach-point="_labelNode">Product Type</label>
<div class="pvrPropertyEditor" data-dojo-attach-point="_editorNode">
<div class="dijitReset dijitInline idxComposite idxFilteringSelectWrap dijitValidationTextBox idxFilteringSelectWrapRequired dijitValidationTextBoxRequired dijitRequired idxFilteringSelectWrapIncomplete dijitValidationTextBoxIncomplete dijitIncomplete idxFilteringSelectWrapIncompleteRequired dijitValidationTextBoxIncompleteRequired dijitIncompleteRequired pvrEditor pvrFilteringSelectEditor" id="widget_pvr_widget_editors_FilteringSelectEditor_0" widgetId="pvr_widget_editors_FilteringSelectEditor_0">
</div>
</div>
</div>
<div class="pvrProperty pvrLayoutItem pvrPropertyRequired" id="pvr_widget_Property_4" widgetId="pvr_widget_Property_4">
<label class="pvrPropertyLabel" id="pvr_widget_Property_4_label" for="pvr_widget_editors_FilteringSelectEditor_1" data-dojo-attach-point="_labelNode">Document Type</label>
<div class="pvrPropertyEditor" data-dojo-attach-point="_editorNode">
<div class="dijitReset dijitInline idxComposite idxFilteringSelectWrap dijitValidationTextBox idxFilteringSelectWrapRequired dijitValidationTextBoxRequired dijitRequired idxFilteringSelectWrapIncomplete dijitValidationTextBoxIncomplete dijitIncomplete idxFilteringSelectWrapIncompleteRequired dijitValidationTextBoxIncompleteRequired dijitIncompleteRequired pvrEditor pvrFilteringSelectEditor" id="widget_pvr_widget_editors_FilteringSelectEditor_1" widgetId="pvr_widget_editors_FilteringSelectEditor_1">
</div>
</div>
</div>
<div class="pvrProperty pvrLayoutItem pvrPropertyRequired" id="pvr_widget_Property_5" widgetId="pvr_widget_Property_5">
<label class="pvrPropertyLabel" id="pvr_widget_Property_5_label" data-dojo-attach-point="_labelNode">Document Type Others</label>
<div class="pvrPropertyEditor" data-dojo-attach-point="_editorNode">
<input type="textbox"></input>
</div>
</div>

使用javascript

query(".pvrPropertyLabel").forEach(function(node){
        var labelName=node.innerHTML;
        if(labelName=="Document Type"){
            alert("lableName"+labelName);
            filtertingSelectId=domAttr.get(node,"for");
            var temp=dijit.byId(filtertingSelectId).get('value');
            alert("value of filtering select is:"+temp);
        }
        else if(labelName=="Document Type Others"){
            alert("lableName"+labelName);
            //alert("query(labelNodes[i])"+query(labelNodes[i]));
            alert("equality test"+node===query(node));
            var parentNode=query(node).closest(".pvrProperty");/*node.closest(".pvrProperty") works fine here but not query(node)*/
            var spanNode=query(node).closest(".pvrPropertyLabelWrapper");
            domStyle.set(spanNode,{"width":"175px"});
            var propEditnode=dijit.byId(filtertingSelectId);
            alert("parentNode:"+parentNode);
            alert("parentNode id"+domAttr.get(parentNode,"id"));
            //var dropdownId=domAttr.get(labelNodes[i],"for");
            if(propEditnode.get('value')=="Other"){
                alert("other");
                domClass.remove(parentNode,"pvrLayoutItemHidden");
                domClass.remove(parentNode,"pvrPropertyHidden");
                domClass.add(parentNode,"pvrLayoutItem");
            }else{
                alert("not other");
                domClass.add(parentNode,"pvrLayoutItemHidden");
                domClass.add(parentNode,"pvrPropertyHidden");
                domClass.remove(parentNode,"pvrLayoutItem");
            }
        }
    });

即使我尝试了相等测试来检查两个节点是否相同,但它们不是。如果我在上面的代码中做了简单的node.closest('')在firefox中运行正常但是当我使用查询(节点).closest('' )在Firefox中也不起作用。不知道我在这里缺少什么。任何帮助都非常感谢。谢谢

答案

我不得不使用polyfill for IE浏览器来支持最接近的方法。下面的代码为我解决了这个问题。希望这有助于面临类似问题的人。

        if (window.Element && !Element.prototype.closest) {
              Element.prototype.closest = 
              function(s) {
                  var matches = (this.document || this.ownerDocument).querySelectorAll(s),
                      i,
                      el = this;
                  do {
                      i = matches.length;
                      while (--i >= 0 && matches.item(i) !== el) {};
                  } while ((i < 0) && (el = el.parentElement)); 
                  return el;
              };
            }
        query(".pvrPropertyLabel").forEach(function(node){
            var labelName=node.innerHTML;
            if(labelName=="Document Type"){
                //alert("lableName"+labelName);
                filtertingSelectId=domAttr.get(node,"for");
                var temp=dijit.byId(filtertingSelectId).get('value');
                //alert("value of filtering select is:"+temp);
            }
            else if(labelName=="Document Type Others"){
                //alert("lableName"+labelName);
                //alert("query(labelNodes[i])"+query(labelNodes[i]));
                var lableId=domAttr.get(node,"id");
                //alert("labelId:"+lableId);
                var parentNode=node.closest(".pvrProperty");
                var spanNode=node.closest(".pvrPropertyLabelWrapper");
                domStyle.set(spanNode,{"width":"175px"});
                var propEditnode=dijit.byId(filtertingSelectId);
                //alert("parentNode:"+parentNode);
                //var dropdownId=domAttr.get(labelNodes[i],"for");
                if(propEditnode.get('value')=="Other"){
                    domClass.remove(parentNode,"pvrLayoutItemHidden");
                    domClass.remove(parentNode,"pvrPropertyHidden");
                    domClass.add(parentNode,"pvrLayoutItem");
                }else{
                    domClass.add(parentNode,"pvrLayoutItemHidden");
                    domClass.add(parentNode,"pvrPropertyHidden");
                    domClass.remove(parentNode,"pvrLayoutItem");
                }
            }
        });

以上是关于dojoquery与最近的方法无法在Internet Explorer中工作的主要内容,如果未能解决你的问题,请参考以下文章

浏览器打开网页正常,但在提交表单时,浏览器无法响应?

在 Xamarin 表单中检测 iOS 的应用空闲时间段

使用一个登录凭据的服务

20190520月考知识总结

HttpOnly是怎么回事?

Fragment 与 Parcel 一起崩溃:调用 onPause 方法时无法编组值错误