我有这个程序,允许用户在双击 div 时编辑它。我试图只让最近双击的 div 有边框。我现在正在使用 addClass 方法执行此操作,我使用此函数添加 .selceted 类:
$(function () { $("div").dblclick(function (e) { clickedTD = event.target; $(clickedTD).find(clickedTD).last.removeClass("selected").addClass("selected"); }
我试图用这个删除最后一个选定的 div,.find(clickedTD).last.removeClass("selected")
以便最近双击的 div 是唯一具有 .selected 类的 div。但这不起作用,我不确定为什么。
这是我的完整代码:
var text;
var selectedText;
var blue = document.getElementById("blue");
var blue2 = document.getElementById("blue2");
var elementCounter = 0;
function addElement() {
var classN = event.target.id;
text = document.getElementById("input").value;
// create a new div element and give it a unique id
var newDiv = document.createElement("div");
newDiv.id = 'temp'+elementCounter;
newDiv.classList = "div";
elementCounter++
if (classN == "blue"){
newDiv.classList = "blue"
} else if (classN == "red"){
newDiv.classList = "red"
} else if (classN == "green"){
newDiv.classList = "green"
} else if (classN == "blue2"){
newDiv.classList = "blue2"
}
// and give it some content
var newContent = document.createTextNode(text);
// add the text node to the newly created div
newDiv.appendChild(newContent);
// add the newly created element and its content into the DOM
var currentDiv = document.getElementById("div1");
document.body.insertBefore(newDiv, currentDiv);
$(function() {
var currentlyDragged;
$("div").draggable({
drag: function (e) {
currentlyDragged = e.target.id
selectedText = event.target;
text = $(selectedText).html();
}
});
$(function () {
$("div").dblclick(function (e) {
clickedTD = event.target;
$(clickedTD).find(clickedTD).last.removeClass("selected").addClass("selected");
}
);
});
});
document.getElementById("input").value = " ";
}
ITMISS
相关分类