3 回答

TA貢獻1909條經驗 獲得超7個贊
我不知道為什么每個人都把它復雜化。首先給表單和按鈕一個 id。然后定義一個隱藏的默認表單 css 類。定義另一個使其可見的類:
<style>
.invisible {
display:none;
}
.visible {
display:block !important;
}
</style>
現在我們添加一個文檔偵聽器,使事情變得更簡單,而不是擺弄任何事件......
document.addEventListener(" click ", function(event) {
//try to get a id even when the event element has not
// directly a id but maybee his parent the form
try {
var id = event.target.id;
if (id === "") {
for (i = 0; i < event.path.length; i++) {
id = event.path[i].id;
if (id !== "") {
if (id === "formid") {
break;
}
}
}
} catch(ex){
console.log(ex);
return;
}
var form=document.getElementById("formid");
switch(id){
case "showcommehtbuttonid":
case "formid":
form.classList.add("visible");
break;
default:
form.classList.remove("visible");
}
}
在您的情況下,切換狀態有一個缺點 - 處理起來很復雜。通過簡單的 id 和按鈕效果最佳。那時是毫無疑問的。添加和刪除處理程序也沒有實際意義。用戶單擊表單或按鈕,表單將變為可見?;蛘咚c擊別的東西。在這種情況下,不會選擇 id 或“錯誤”的 id。在這種情況下,默認切換規則使表單不可見。這個東西未經測試,可能需要一些小的調整。最好的 - 在事件處理程序中,您現在還可以添加非常簡單的更多操作。

TA貢獻1810條經驗 獲得超5個贊
我試圖重構代碼。我首先將代碼清理成函數,而不是嵌套單擊事件。在重構時,我使用console.log's 來幫助我確定 何時處于commentMode活動狀態 - 然后觸發showForm()或hideForm()功能。對于一個工作示例:https : //codepen.io/lachiekimber/pen/bGGNYmK
$(function () {
var imageWrapper = $('#imageWrapper');
var form = $('.new-comment');
var x, y, formX, formY;
var newComment = true;
var commentMode = false;
$('#newComment').click(function() {
imageWrapper.toggleClass('new-comment-pointer');
commentMode = !commentMode;
//console.log('newComment');
});
$('#imageWrapper').click(function(e) {
if(commentMode) {
//console.log('commentMode: true');
showForm(e);
} else {
//console.log('commentMode: false');
hideForm();
}
});
function showForm(e) {
getCoordinates(e, imageWrapper);
form.show().css({'left': formX, 'top': formY});
form.find('textarea').focus();
form.find('#xAxis').val(x); // x is from the getCoordinates
form.find('#yAxis').val(y); // y is from the getCoordinates
}
function hideForm() {
form.hide();
}
function getCoordinates(e, image) {
var offset = image.offset();
x = e.pageX - offset.left;
y = e.pageY - offset.top;
formX = x + 50;
formY = y + 20;
}
});

TA貢獻1776條經驗 獲得超12個贊
工作和測試:)!好的,我們可以在頁面樣式上做很多工作,但是 :d 看看整個頁面看起來也很簡單。我不必為這個小任務與 jquery 爭吵 :) 我也僅通過純 html 設置了自動對焦。
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
<style>
.invisible {
display: none;
border: 1px solid red;
}
.visible {
display: block !important;
}
</style>
</head>
<body>
<form id="formid" class="invisible">
<h1>Form</h1>
<input type="text" name="size" autofocus>
</form>
<hr>
<input type="button" value="click" id="showcommentbuttonid">
<script>
document.addEventListener("click", function(event) {
try {
var id = event.target.id;
if (id === "") {
for (i = 0; i < event.path.length; i++)
{
id = event.path[i].id;
if (id !== "") {
if (id === "formid") {
break;
}
}
}
}
} catch (ex) {
console.log(ex);
return;
}
var form = document.getElementById("formid");
switch (id) {
case "showcommentbuttonid":
case "formid":
form.classList.add("visible");
break;
default:
form.classList.remove("visible");
}
}
)
</script>
</body>
</html>
添加回答
舉報