如何获取 jQuery 中事件目标元素的标识符?
How to get identifier of an event target element in jQuery?
我需要在 db/localstorage 中存储事件目标元素,在重新加载时我需要根据更改更新它们的属性。例如:原本是红色的文字,点击时需要将其属性改为蓝色。重新加载时,蓝色应替换 Javascript (jQuery) 中的红色。问题是,我们无法保证元素是否具有 id 属性。我们如何将元素唯一标识符存储在数据库中并稍后将其用于更新?
$("body *").click(function (evt) {
console.log($(this), 'this');
console.log($(evt.target), 'evt target', $(evt.target)[0]);
$(evt.target).css('color', 'blue');
//storing event.target in local storage here. with changed property
}
$("body").ready(function() {
//retrieve elements from db.
});
The issue is, we can't guarantee if the element has id attribute.
The program should work on any page (Any client who uses our SDK).
没有。你根本做不到。识别元素的唯一方法是使用 ID 或某种 unique/group 标识符。如果你不能保证它们,你就无法识别它们。
实现此目的的方法
我需要在 db/localstorage 中存储事件目标元素,在重新加载时我需要根据更改更新它们的属性。例如:原本是红色的文字,点击时需要将其属性改为蓝色。重新加载时,蓝色应替换 Javascript (jQuery) 中的红色。问题是,我们无法保证元素是否具有 id 属性。我们如何将元素唯一标识符存储在数据库中并稍后将其用于更新?
$("body *").click(function (evt) {
console.log($(this), 'this');
console.log($(evt.target), 'evt target', $(evt.target)[0]);
$(evt.target).css('color', 'blue');
//storing event.target in local storage here. with changed property
}
$("body").ready(function() {
//retrieve elements from db.
});
The issue is, we can't guarantee if the element has id attribute.
The program should work on any page (Any client who uses our SDK).
没有。你根本做不到。识别元素的唯一方法是使用 ID 或某种 unique/group 标识符。如果你不能保证它们,你就无法识别它们。