[英]Make body behave like a button when I click anywhere and save
I have a text area, button to save the text that is type inside the text area and some other stuff on the page.我有一个文本区域,用于保存在文本区域内键入的文本的按钮以及页面上的一些其他内容。
I want to click outside of the body and save the text that I type inside the text area;我想在正文外部单击并保存我在文本区域内键入的文本; my body should act as button.
我的身体应该充当按钮。
My problem is it does save the information but it affects everything that I have on my page eg wherever I click, it saves and I want it not to save when I click some other button - it should focus only outside the element.我的问题是它确实保存了信息,但它会影响我页面上的所有内容,例如,无论我点击什么地方,它都会保存,我希望它在我点击其他按钮时不保存——它应该只关注元素之外。
What should I do to focus only outside the element because I even try function called stopPropagation.我应该怎么做才能只关注元素外部,因为我什至尝试了 function 调用 stopPropagation。
var text_a = document.getElementById("myTextarea"); var btn_saveComment = document.getElementById("submit2"); function saveComments() { text_a.style.display = "block"; $("#comment").text($(text_a).val()); var g = document.getElementById("checks5"); g.checked = true; g.disabled = false; $("#checks5").css({ backgroundColor: "#C40929" }) } document.addEventListener("click", function(e) { e.stopPropagation(); text_a.style.display = "block"; $("#comment").text($(text_a).val()); var g = document.getElementById("checks5"); g.checked = true; g.disabled = false; $("#checks5").css({ backgroundColor: "#C40929" }) })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <div class="wrapper"> <input type="checkbox" class="check_ticks" id="checks5"> <label for="checks5"></label> </div> <p id="comment" class="text_info"></p> <textarea id="myTextarea" rows="5" cols="30" class="myText"></textarea> <button class="dropbtn" id="submit2" onclick="saveComments()">Save Comment</button>
You can simply add onBlur
attribute to the text area.您可以简单地将
onBlur
属性添加到文本区域。 Like this:像这样:
<textarea id="myTextarea" rows="5" cols="30" onblur="saveComments()" class="myText"></textarea>
See demo below:请参阅下面的演示:
var text_a = document.getElementById("myTextarea"); var btn_saveComment = document.getElementById("submit2"); function saveComments() { console.log('saving...'); text_a.style.display = "block"; $("#comment").text($(text_a).val()); var g = document.getElementById("checks5"); g.checked = true; g.disabled = false; $("#checks5").css({ backgroundColor: "#C40929" }); console.log('saved'); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <div class="wrapper"> <input type="checkbox" class="check_ticks" id="checks5"> <label for="checks5"></label> </div> <p id="comment" class="text_info"></p> <textarea id="myTextarea" rows="5" cols="30" onblur="saveComments()" class="myText"></textarea> <button class="dropbtn" id="submit2" onclick="saveComments()">Save Comment</button>
Using simple vanilla Javascript you can use the event
to aid the process of using the document Body to work as the button in the original code by inspecting the event.target
and event.currentTarget
properties.使用简单的 vanilla Javascript,您可以通过检查
event.target
和event.currentTarget
属性,使用event
来帮助使用文档 Body 作为原始代码中的按钮的过程。 When the event listener is bound to the body ( as below ) the currentTarget is the body element itself rather than any child element so that allows you to process the click on that itself quite easily.当事件侦听器绑定到主体时(如下所示),currentTarget 是主体元素本身而不是任何子元素,因此您可以非常轻松地处理对其本身的点击。
( css to make this easier to observe within the snippet ) ( css 以便在代码片段中更容易观察到)
const d=document; d.body.addEventListener('click',e=>{ let oText=d.querySelector('textarea#myTextarea'); if( e.target==e.currentTarget && oText.value.='' ){ let oPara=d;querySelector('p#comment'). oPara.textContent=oText;value. let oChk=d;querySelector('input#checks5'). oChk;checked=true. oChk;disabled=false. oChk.style.backgroundColor="#C40929" } })
body{ height:100vh; width:100%; padding:0; margin:0; }
<div class="wrapper"> <input type="checkbox" class="check_ticks" id="checks5"> <label for="checks5"></label> </div> <p id="comment" class="text_info"></p> <textarea id="myTextarea" rows="5" cols="30" class="myText"></textarea>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.