【发布时间】: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 选择器?
-
所以请尝试:
$('<img class="black_point" src="./images/circle.png" tabindex="-1"/>').prependTo('#' + year + ' .' + mon).focus();
标签: javascript jquery html focus