简体   繁体   English

当我点击任何地方并保存时,让身体像一个按钮一样

[英]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.targetevent.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.

相关问题 当我单击正文中的任何位置时,jQuery会自动隐藏 - When I click anywhere in body toggle jQuery hide automatically 单击按钮时显示菜单,单击页面或按钮上的任意位置时隐藏菜单 - Show menu when i click on button and hide that when i click anywhere on the page or button 单击任何地方关闭菜单; 单击任何位置时如何使其停止打开 - click anywhere close menu; how do I make it stop opening when clicked anywhere 使asp:LinkBut​​ton表现得像asp:button - Make asp:LinkButton behave like asp:button 如何使按钮具有这种行为? - How can make a button behave like this? 如何使整行以及其中的复选框都可以单击,然后在我在表行内部单击时单击并取消单击? - How to make entire row along with the checkbox in it, click and unclick when I anywhere click inside the table row? 如何防止fancybox关闭? 想要使用户单击“关闭”按钮,而不是在浏览器上的任何位置 - How can I prevent fancybox from closing? Want to make the user click the Close button instead of anywhere on the browser 我如何使按钮的行为像后退箭头键一样? - How could i make a button behave like the back arrow key does? 当鼠标单击正文中的任何位置时,页面向上滚动 - Page Scroll Up when mouse click on anywhere in body 单击以表现类似返回 - Click to behave like a Return
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM