简体   繁体   中英

The javascript function srcElement.nodeName gives wrong output in iOS5

The following JavaScript function works on iOS 4 but gives the wrong output on iOS 5:

var attribs = event.srcElement.attributes;
    myWebPageElement = event.srcElement;
    node = event.srcElement.nodeName;

if(node == "#text")
    alert('Touch event occured on Text.');
else 
    alert('Touch event occurred on image.');

In iOS 5, regardless of whether the event occurred on text or on an image, JavaScript always executes the else block, as each time we get the tag corresponding to text and we don't get the nodename "#text" .

How can I fix this problem?

Do not use srcElement to check the target of your events, instead use target or currentTarget. Using these will give you more consistency between browser versions.

srcElement is Microsoft's addition, as usual they add stuff that nobody else uses. See here for more http://www.quirksmode.org/dom/w3c_events.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM