开发者

Dojo Issue: .parent() not a function

开发者 https://www.devze.com 2023-04-11 04:05 出处:网络
The HTML snippet: <div class=\"hide_on_start\"> <label&开发者_如何学Cgt;Type of Visit</label>

The HTML snippet:

<div class="hide_on_start">
    <label&开发者_如何学Cgt;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


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


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');


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().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号