Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找出现在DOM html中的最后一个元素_Javascript_Jquery_Html_Focus - Fatal编程技术网

Javascript 查找出现在DOM html中的最后一个元素

Javascript 查找出现在DOM html中的最后一个元素,javascript,jquery,html,focus,Javascript,Jquery,Html,Focus,我有一个将图像放在表格(html)上的代码,我想把重点放在刚刚出现的图像上。 我知道我必须使用$(这个),但我不知道如何使用,这是我的代码 function positioning(year, mon) { $('#' + year + ' .' + mon).prepend('<img class="black_point" src="./images/circle.png"/>');//that adds the image var table = documen

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

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;
    } 
}
功能定位(年、月){
$('#'+year+'.+mon).prepend('');//这将添加图像
var table=document.getElementById(“表格”);
var images=table.getElementsByTagName(“img”);
//这里我需要我刚刚添加的当前图像发送到该函数
功能连接(图像){
var tabBcr=table.getBoundingClientRect();
var imgBcr=image.getBoundingClientRect();
x=imgBcr.left+(imgBcr.width/2)-选项卡bcr.left;
y=imgBcr.top+(imgBcr.height/2)-选项卡bcr.top;
} 
}

我希望我已经解释得很好。

我认为它会起作用,在您想要获得img元素的地方添加以下内容:

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

如果多次调用
positioning()
方法,则呈现的HTML标记无效。ID在文档上下文中必须是唯一的。仅供参考,默认情况下,图像不是可聚焦的输入,您需要设置一个tabindex属性以使其可聚焦a.Wolff所说的是正确的。一旦解决了这个问题,您可以做的就是在将图像附加到DOM之前在变量中创建图像。然后在你附加了它之后,调用该变量的.focus()。是的,很抱歉我弄糊涂了,不是id也是类,只是因为它让我有点烦。为什么要混合使用javascript选择器和jquery选择器?请尝试:
$('').prependTo('#'+year+'.+mon).focus()是的,它可以工作,现在我在imgelem中有了图像,不是吗?嗯,但是现在,我正在做:
$('#'+anio+'.+mon.).prepend('');var imgelem=$('.+anio+'.+mon).find(“img:first”);var position=imgelem.position();警报(“左:“+position.left+”,顶部:“+position.top”)但无法识别留下的属性,可能是因为imgelem?编写的内容与预先编写时的内容相同<代码>变量imgelem=$('#'+anio+'..+mon).find(“img:first”)