【问题标题】:Find the last element that has appeared DOM html找到最后一个出现 DOM html 的元素
【发布时间】:2015-05-06 11:55:22
【问题描述】:

我有一个将图像放在表格(html)上的代码,我想专注于刚刚出现的图像。 我知道我必须使用 $(this) 但我不知道如何,这是我的代码

function positioning(year, mon) {
    $('#' + year + ' .' + mon).prepend('<img class="black_point" src="./images/circle.png"/>');//that adds the image
    var table = document.getElementById("table");
    var images = table.getElementsByTagName("img");
    //here I need the current image I had just add to send to that function
    function connect(images) {
        var tabBcr = table.getBoundingClientRect();
        var imgBcr = image.getBoundingClientRect();
        x = imgBcr.left + (imgBcr.width / 2) - tabBcr.left;
        y = imgBcr.top + (imgBcr.height / 2) - tabBcr.top;
    } 
}

希望我已经解释清楚了。

【问题讨论】:

  • 如果您多次调用positioning() 方法,您将呈现无效的HTML 标记。 ID 在文档上下文中必须是唯一的。仅供参考,默认情况下图像不是可聚焦的输入,您需要设置 tabindex 属性以使其可聚焦
  • A.Wolff 说的是真的。一旦你解决了这个问题,你可以做的是在将图像附加到 DOM 之前在变量中创建图像。然后在你附加它之后,在那个变量上调用 .focus()。
  • 是啊,对不起我弄糊涂了,是不是id是类
  • 另外,只是因为它有点困扰我。为什么要混合使用 javascript 选择器和 jquery 选择器?
  • 所以请尝试:$('&lt;img class="black_point" src="./images/circle.png" tabindex="-1"/&gt;').prependTo('#' + year + ' .' + mon).focus();

标签: javascript jquery html focus


【解决方案1】:

我认为它会起作用,将其添加到您想要获取该 img 元素的位置:

var imgelem=$('#' + year + ' .' + mon).find("img:first");

【讨论】:

  • 是的,它有效,现在我在 imgelem 中有图像不是吗?
  • 嗯,但是现在,我正在这样做:$('#' + anio + ' .' + mon).prepend('&lt;img class="black_point" src="./images/circulo.png"/&gt;'); var imgelem=$('.' + anio + ' .' + mon).find("img:first"); var position = imgelem.position(); alert( "left: " + position.left + ", top: " + position.top ); 但无法识别剩下的属性,也许这可能是 imgelem 的原因?
  • 写入与添加时相同.. var imgelem=$('#' + anio + ' .' + mon).find("img:first");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 2021-11-15
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多