简体   繁体   English

用JavaScript覆盖DOM元素

[英]Override DOM elements with javascript

In a nutshell I'm trying to target an element within the DOM, then inject a class on the fly to later alter that element. 简而言之,我试图在DOM中定位一个元素,然后动态注入一个类以稍后更改该元素。

The situation is as follows, I am working with an application that has predetermined mark up (Manage Engine). 情况如下,我正在使用具有预定标记的应用程序(管理引擎)。 It's a tool for system work flows, creating a centralized portal for ticket logging, asset management blah blah. 它是用于系统工作流程的工具,可创建用于票务记录,资产管理等等的集中式门户。 So I use the tool to create templates for end users to log service requests. 因此,我使用该工具为最终用户创建模板来记录服务请求。 This is accessed via a web interface portal which in turn obviously has mark up. 这可以通过Web界面门户访问,该门户显然又具有标记。

So far I have been able to alter specific things such as background colors on table headers for example. 到目前为止,我已经能够更改特定的内容,例如表格标题上的背景色。 I achieve this by creating a rule to fire within that template upon load time. 我通过创建在加载时在该模板内触发的规则来实现这一点。 So essentially I am allowing the template to load with its predetermined code and then I am applying a for loop to alter the code once it has loaded. 因此,从本质上讲,我允许模板加载其预定的代码,然后在模板加载后应用for循环更改代码。 (Hacky I know, however its working really well). (我很高兴,但是它确实运行良好)。

The issue I'm running into now is that certain things within the mark up are generic (no class or id associated to the element). 我现在遇到的问题是标记中的某些内容是通用的(没有与元素相关联的类或ID)。 My plan is to target that specific generic element as a variable then add my own class to it upon load. 我的计划是将特定的通用元素作为变量作为目标,然后在加载时向其添加我自己的类。 Is there a way to target an element that has a class and then target the child elements within, save that child as a variable to then add a class on the fly with javascript. 有没有一种方法可以针对具有类的元素,然后针对其中的子元素,将该子对象另存为变量,然后使用javascript快速添加类。 Please see example below. 请参见下面的示例。

<tr class=test1>   
  <td>
   <input>
   <input>
   <input>
  </td>
<tr/>

So with the example above what I am trying to achieve is add my own class with JavaScript to the <td> element. 因此,在上面的示例中,我试图实现的是将自己的JavaScript类添加到<td>元素中。 Obviously if i target just <td> it will alter all <td> elements within the markup. 显然,如果我仅定位<td> ,它将更改标记中的所有<td>元素。 Can i get to that specific <td> via the <tr> parent with the test1 class. 我可以通过test1类的<tr>父级到达那个特定的<td>吗? I am currently unable to use any jquery requests as the base code can not be touched. 我目前无法使用任何jquery请求,因为无法触摸基本代码。

Again I know this is a little backwards and hacky but it does work with anything I can specifically target (has a class or id). 再一次,我知道这有点落后和棘手,但是它确实可以与我可以专门针对的任何对象一起使用(具有类或id)。 I need to be able to do this with pure JavaScript. 我需要能够使用纯JavaScript做到这一点。 Any suggestions or help is greatly appreciated, apologies if this is a noob approach or question, first time posting in a forum. 任何建议或帮助都将不胜感激,如果这是菜鸟方法或问题,我们将在第一次论坛上致歉。 Let me know if further examples or information is required. 让我知道是否需要更多示例或信息。

document.querySelector(“ body”)。children可以获取body的所有子元素

step1: Select the class. 步骤1:选择班级。 The variable t1 will contain an Array of tr elements. 变量t1将包含一个tr元素数组。

var t1 = document.querySelector('.test1');

step2: Get the first value from the array t1 . 步骤2:从数组t1获取第一个值。 So, tr_el1 contains one tr element. 因此, tr_el1包含一个tr元素。

var tr_el1 = t1[0];

step3: Get the children of tr . 第三步:得到tr的孩子。 td_el contains an Array of td elements. td_el包含td元素数组。

var td_el = tr_el1.children;

Now you can use the td from the td_el array 现在您可以使用td_el数组中的td

var tr = document.getElementsByClassName('test1')[0]
var td = tr.children[0]
var inputs = Array.prototype.slice.apply(td.children)

Now you got your inputs inside an array. 现在,您将输入放入数组中。 You're welcome ;-) 别客气 ;-)

Thanks a lot for the assistance, really appreciate it. 非常感谢您的帮助,非常感谢。 The examples above helped me build a hybrid that although not exact has given me the outcome i needed. 上面的示例帮助我构建了一种混合动力,尽管不确定性可以给我带来所需的结果。

var parent = document.querySelector(".roweven").children;
var nS;
for (nS = 0; nS < parent.length; nS++) {

parent[nS].style.position = "absolute";
parent[nS].style.width = "100%";
} 

I am yet to wrap this up in a function but working as intended. 我尚未将其包装在一个函数中,但按预期工作。 Thanks again :) :) 再次感谢 :) :)

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

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