Dojo Issue: .parent() not a function

孤街浪徒 提交于 2019-12-23 02:47:05

问题


The HTML snippet:

<div class="hide_on_start">
    <label>Type of Visit</label>
    <div id="record_visit_type"></div>
</div>
<div class="hide_on_start">
    <label>Visit Date</label>
    <div id="record_visit_date"></div>
</div>
<div class="hide_on_start">
    <label>Staff</label>
    <div id="record_staff"></div>
</div>

The javascript I am using:

>>> dojo.byId('record_visit_type')
<div id="record_visit_type">

>>> dojo.byId('record_visit_type').parent().removeClass('hide_on_start')
TypeError: dojo.byId("record_visit_type").parent is not a function

I don't understand what the issue is with dojo.byId('record_visit_type').parent().removeClass('hide_on_start'). Can somebody explain?

Thanks


回答1:


Theres a couple of problems I see with your code:

I think what you are looking for is the parentNode property of the domNode you are retrieving. This is not a method, but a property of the domNode you are looking up via dojo.byId.

Also, domNodes themselves to not have a removeClass method. You probably want to use dojo's dojo.removeClass(domNOde, cssClass) method to do this.

var recordVisitTypeDomNode = dojo.byId('record_visit_type');
dojo.removeClass(recordVisitTypeDomNode.parentNode, 'hide_on_start');



回答2:


It looks like you're using dojo.byId as if it returns a dojo.NodeList, but it doesn't - it just returns a DOM node. Only dojo.query regularly returns dojo.NodeList objects.

dojo.NodeList objects have a removeClass function (which operates on all nodes in the list), and if you dojo.require("dojo.NodeList-traverse"), they also have a parent() function which returns a new NodeList containing the immediate parents of respective nodes in the original list.

http://dojotoolkit.org/reference-guide/dojo/NodeList-traverse.html




回答3:


parentNode is right but here is how you do it in dojo:

// Go from the DOM node to a NodeList
var myDomNode = dojo.byId('record_visit_type');
var myNodeList = dojo.query(myDomNode);

// Get the parent
dojo.require("dojo.NodeList-traverse");
var parent = myNodeList.parent()[0];

This method of calling dojo.query is valid:

//        Non-selector Queries:
//        ---------------------
//
//        If something other than a String is passed for the query,
//        `dojo.query` will return a new `dojo.NodeList` instance
//        constructed from that parameter alone and all further
//        processing will stop. This means that if you have a reference
//        to a node or NodeList, you can quickly construct a new NodeList
//        from the original by calling `dojo.query(node)` or
//        `dojo.query(list)`.

http://jsapi.info/dojo/1/dojo.query

It is like jquery $(myDomNode).parent().



来源:https://stackoverflow.com/questions/7664781/dojo-issue-parent-not-a-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!