3 回答

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊
我不知道為什么每個(gè)人都把它復(fù)雜化。首先給表單和按鈕一個(gè) id。然后定義一個(gè)隱藏的默認(rèn)表單 css 類。定義另一個(gè)使其可見的類:
<style>
.invisible {
display:none;
}
.visible {
display:block !important;
}
</style>
現(xiàn)在我們添加一個(gè)文檔偵聽器,使事情變得更簡(jiǎn)單,而不是擺弄任何事件......
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");
}
}
在您的情況下,切換狀態(tài)有一個(gè)缺點(diǎn) - 處理起來(lái)很復(fù)雜。通過(guò)簡(jiǎn)單的 id 和按鈕效果最佳。那時(shí)是毫無(wú)疑問(wèn)的。添加和刪除處理程序也沒有實(shí)際意義。用戶單擊表單或按鈕,表單將變?yōu)榭梢??;蛘咚c(diǎn)擊別的東西。在這種情況下,不會(huì)選擇 id 或“錯(cuò)誤”的 id。在這種情況下,默認(rèn)切換規(guī)則使表單不可見。這個(gè)東西未經(jīng)測(cè)試,可能需要一些小的調(diào)整。最好的 - 在事件處理程序中,您現(xiàn)在還可以添加非常簡(jiǎn)單的更多操作。

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
我試圖重構(gòu)代碼。我首先將代碼清理成函數(shù),而不是嵌套單擊事件。在重構(gòu)時(shí),我使用console.log's 來(lái)幫助我確定 何時(shí)處于commentMode活動(dòng)狀態(tài) - 然后觸發(fā)showForm()或hideForm()功能。對(duì)于一個(gè)工作示例: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貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊
工作和測(cè)試:)!好的,我們可以在頁(yè)面樣式上做很多工作,但是 :d 看看整個(gè)頁(yè)面看起來(lái)也很簡(jiǎn)單。我不必為這個(gè)小任務(wù)與 jquery 爭(zhēng)吵 :) 我也僅通過(guò)純 html 設(shè)置了自動(dòng)對(duì)焦。
<!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>
添加回答
舉報(bào)