简体   繁体   English

跨浏览器导航JavaScript中的DOM元素

[英]Cross browser navigation of DOM elements in Javascript

A teammate of mine wrote some code about a short time ago which navigated about the DOM elements in out HTML page to pre-fill some fields in a modal based on the already existing data in an object (the modal allowed a user to edit that data). 我的一个队友在不久前写了一些代码,该代码在HTML页面中浏览DOM元素,以基于对象中已经存在的数据来预先填充模式中的某些字段(该模式允许用户编辑该数据)。 The items are generated generically from a database table. 这些项目通常是从数据库表生成的。

function showModal(editImage) {
    var modal = document.getElementById('myModal');
    var span = document.getElementsByClassName("close")[0];
    var nameAndTitle = editImage.srcElement.parentElement.innerHTML;
    var parent = editImage.srcElement.parentElement.parentElement;
    etc....

The problem is, they only tested that it worked in Chrome. 问题是,他们只测试了它在Chrome中的工作能力。 The code never worked in firefox, it seems. 看来,该代码从未在firefox中起作用。 When I try to open one of the modals in firefox, I get the console output "TypeError: editImage.srcElement is undefined" 当我尝试在firefox中打开模态之一时,得到控制台输出“ TypeError:editImage.srcElement未定义”

My question is, is there a more "correct" way to access this data that will work for any browser, or do I need to check what browser I am in and access that information in a different way depending on the browser being used? 我的问题是,是否存在一种更“正确”的方式来访问适用于任何浏览器的数据,还是我需要检查我所在的浏览器并根据所使用的浏览器以不同的方式访问该信息?

Your immediate answer is: change srcElement to target . 您的直接答案是:将srcElement更改为target The Mozilla Developer Network is a very good (one of many) resource to check for standards compliance. Mozilla开发人员网络是检查标准符合性的很好的资源(众多资源之一)。 A visit to their site for srcElement indicates that it is non-standard and makes the suggestion on the correct way ( target ). 访问他们srcElement 网站表示这是非标准的,并以正确的方式提出了建议( target )。

Unfortunately, even APIs that are standard don't always work in all browsers. 不幸的是,即使是标准的API也不总是在所有浏览器中都能使用。 Usually, parts of a standard are implemented piecemail. 通常,部分标准是通过邮件实现的。 Checking with authoritative sources is vital to know what is supported where. 与权威来源进行核对至关重要,以了解在何处获得支持。

Other resources: 其他资源:

As for your explicit question: 至于您的明确问题:

"My question is, is there a more "correct" way to access this data that will work for any browser, or do I need to check what browser I am in and access that information in a different way depending on the browser being used?" “我的问题是,是否存在一种更“正确”的方式来访问适用于任何浏览器的数据,还是我需要检查我所在的浏览器并根据所使用的浏览器以不同的方式访问该信息? ”

Use standards and check for support (via the resources I've provided above) to have the best chance at cross-browser code. 使用标准并检查支持(通过上面提供的资源),以获得跨浏览器代码的最佳机会。

DO NOT write code that checks the browser type and version to see if your code will run (browser detection) because: 请勿编写用于检查浏览器类型和版本的代码,以查看您的代码是否将运行(浏览器检测),因为:

  1. There are too many browsers and too many versions - this sucks! 浏览器太多,版本太多-很烂!
  2. Browsers can and will lie to you about what they are! 浏览器可以而且会骗你关于它们的含义!

Use "feature detection" when in doubt. 如有疑问,请使用“功能检测”。 Feature detection is code that evaluates whether a feature exists and uses it if it does. 功能检测是评估功能是否存在并使用(如果存在)的代码。 If it doesn't a fallback is provided. 如果没有,则提供备用。 Here's a very common one for IE8 (and lower) browsers that did not yet support the W3C standard for event handling: 对于不支持事件处理的W3C标准的IE8(及更低版本)浏览器,这是一个非常常见的浏览器:

// Here we are attempting to obtain the value of the 
// addEventListener property of the window object.
// IE 8 doesn't implement this property so "undefined"
// will be returned. But, because we are attempting to
// use the  value as the condition of an if/then construct
// "undefined" will be converted to a boolean. "undefined"
// is a "falsey" value, so it will convert to false. 
// This means that if the else portion of our construct
// is reached, we have a browser that doesn't support
// addEventListener
if(window.addEventListener){
   // W3C standards are supported - do things the standard way
   obj.addEventListener("click", someFunction, capture);
} else {
   // Must be IE 8 or less - do things the IE way
   obj.attachEvent("onclick", someFunction);
}

This is but one way to use feature detection, but it typically hinges on converting a value to a boolean. 这只是使用特征检测的一种方法,但通常取决于将值转换为布尔值。 See more on it here . 这里查看更多信息

That function showModal is probably an event listener, so the argument editImage is actually an Event object. 该函数showModal可能是事件侦听器,因此参数editImage实际上是一个Event对象。

As such, the actual property that reports the source of the event - and the only one supported by Firefox - is target, while srcElement is a legacy property that was created by Microsoft and Webkit/Blink based browsers kept supporting it for compatibility. 因此,报告事件源的实际属性(也是Firefox支持的唯一属性)是目标,而srcElement是由Microsoft和基于Webkit / Blink的浏览器创建的旧属性,为保持兼容性一直支持该属性。 But not Firefox. 但不是Firefox。

In short: use target or, if you need to support older version of Internet Explorer, try with (editImage.target || editImage.srcElement) . 简而言之:使用target,或者,如果需要支持旧版本的Internet Explorer,请尝试使用(editImage.target || editImage.srcElement)

srcElement is from IE. srcElement来自IE。 The standard property is target . 标准属性是target

You should do this: 你应该做这个:

var target = editImage.srcElement || editImage.target;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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