你好,我正试图获取一个jquery对象。但它总是返回未定义的。我附上了jsfiddle供您参考。https://jsfiddle.net/tsrdkote/
这是我使用的代码
$(document).dblclick(function(e) {
console.log($(this));
if (e.target.attributes.getNamedItem("data-type")) {
if (e.target.attributes.getNamedItem("data-type").value != "group") {
var type = e.target.parentNode.attributes.getNamedItem("data-type").value
object = $(e.target);
if (type == "text") {
var t = document.createAttribute("contenteditable");
t.value = "true";
e.target.attributes.setNamedItem(t);
e.target.focus()
}
}
} else {
var type = e.target.parentNode.attributes.getNamedItem("data-type").value;
object = $(this).parent();
console.log(object)
if (type == "text") {
var t = document.createAttribute("contenteditable");
t.value = "true";
e.target.attributes.setNamedItem(t);
e.target.focus()
}
}
});
在这里,当前元素$(this)总是返回长度为0的JQuery对象。我遗漏了什么吗?
最佳答案
object = $(this).parent();
应该是:object = $(e.target).parent();