简体   繁体   English

mootools获取没有ID的元素的位置

[英]mootools get position of an element which has no ID

I'm relatively new to the whole Javascript thing, except for some basic jQuery stuff. 除了一些基本的jQuery内容外,我对整个Java脚本还是比较陌生的。

Recently I took over a small local project that uses mootools and I'm stuck. 最近,我接手了一个使用mootools的小型本地项目,但我陷入了困境。

I have several pages with a relative simple dom structure and content that is stored in a database. 我有几页具有相对简单的dom结构和存储在数据库中的内容的页面。 The first element on each page is either a div or a paragraph with fixed width and height. 每页上的第一个元素是div或具有固定宽度和高度的段落。

There are three variations of said element, each of them differs in height, which means that we have either one of these on every page 所说元素有3种变化,每种都有不同的高度,这意味着我们在每页上都有一个

<div class="header1"><img ...></div>
<p class="header2"><img ...></p>
<p class="header3"><img ...></p>

Each of these are followed by several paragraphs containing text or images. 每一个后面都有包含文本或图像的几个段落。

My Problem: I have to put a small div with a print icon between the page's first element (div or p) and the following content but I'm not allowed to change any of the code that generates this page. 我的问题:我必须在页面的第一个元素(div或p)和以下内容之间放置一个带有打印图标的小div,但不允许更改生成该页面的任何代码。

The Idea is to use mootools (which is loaded anyway) to place the div on an absolute position just below the first element. 想法是使用mootools(无论如何加载)将div放置在第一个元素正下方的绝对位置。

This could be done quite easily with jQuery, but I have to use mootools plus the elements do not have IDs. 使用jQuery可以很容易地做到这一点,但是我必须使用mootools加上元素没有ID。

Is there any easy way to get the position of either , or on page load and then placing (position: absolute;) a div right under it? 是否有任何简单的方法来获取或在页面加载时的位置,然后将div放置在其下?

So far I have this, (which is quite poor) which logs the the elemnent with a class "header1" 到目前为止,我有这个(相当差),它使用“ header1”类记录元素

window.addEvent('domready', function() {
$$('.header1').each(function(element){
    console.log(element);
});

}); });

element.getPosition() gets you the x and y positions of an element, which should allow you to create a new element positioned around there somewhere. element.getPosition()您提供element.getPosition()的x和y位置,这应允许您在该位置附近创建一个新元素。 getPosition() could be in MooTools More, depending on the version you're using. getPosition()可能位于MooTools More中,具体取决于您使用的版本。

Adding a new element to the DOM at an appropriate place without the tricky positioning might be easier, though that will depend on your DOM. 在适当的位置将新元素添加到DOM中而无需进行棘手的定位可能会更容易,尽管这将取决于您的DOM。


Judging your reactions from comments, if you simply wanted to match a single element in the document by something else than an id, document.getElement() is what you need, which will do about the same as $$() , but return the first match or null if nothing matched. 从注释中判断您的反应,如果您只是想通过id以外的其他内容来匹配文档中的单个元素,则需要document.getElement() ,其效果与$$()相同,但是返回第一次匹配;如果没有匹配项,则返回null

Yes of course. 当然是。 The question is how to get the element you want if it has no ID. 问题是如果没有ID,如何获取所需的元素。 This is as hard as in jQuery or as easy as in jQuery. 这与jQuery一样困难,或者与jQuery一样容易。

Take a look at mootools documentation of Element.getPosition() . 看一下Element.getPosition() mootools文档。

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

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