简体   繁体   English

我可以从子跨度点击事件中获取父Div标签innerHTML吗,请参见代码

[英]Can I get the parent Div tag innerHTML from a child span click event see code

<body>
  <div class='Myclass'>
     Value I Want, I have already got the span inner html, but I want to add the the value of the div
     <span onclick='Some javascript to get div value'>some html</span>
  </div>
</body>

Tell me if I can improve this answer. 告诉我是否可以改善此答案。

Please thank @JoshCrozier for his jsfiddle ( https://jsfiddle.net/1p2j4em7/ ) which imparted some new information for this answer to be the best it could be. 请感谢@JoshCrozier的jsfiddle( https://jsfiddle.net/1p2j4em7/ ),它为该答案提供了一些新信息,力求做到最好。

 function getText(elem){ alert(elem.previousSibling.nodeValue.trim()) } 
 <body> <div class='Myclass'> Value I Want, I have already got the span inner html, but I want to add the the value of the div <span onclick='getText(this);'>some html</span> </div> </body> 

Here is a solution that is generic for as many divs as you want. 这是一个通用的解决方案,可用于所需的多个div。

 window.addEventListener('load', function() { var divs = document.getElementsByClassName('Myclass'); for (var i = 0; i < divs.length; i++) { divs[i].children[0].onclick = function() { alert(this.previousSibling.nodeValue.trim()) } } }); 
 <body> <div class='Myclass'> Value I Want, I have already got the span inner html, but I want to add the the value of the div <span onclick='getText(this);'>some html</span> </div> </body> 

点击事件<span>或</span><div>标签不工作 safari 问题</div><div id="text_translate"><p>我的 iPhone(7 Plus) 上出现了Click事件问题。 点击方法不适用于 IOS 设备。 但在 IOS Chrome 上工作正常。</p><p> 这是代码: </p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $('.menuOpen').on('click', function() { console.log('Show Menu'); $('.menuBar').toggleClass('d-none'); });</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"&gt; &lt;span class="menuOpen"&gt;Menu&lt;/span&gt; &lt;ul class="d-none menuBar"&gt; &lt;li&gt;Action 1&lt;/li&gt; &lt;li&gt;Action 2&lt;/li&gt; &lt;li&gt;Action 3&lt;/li&gt; &lt;li&gt;Action 4&lt;/li&gt; &lt;/ul&gt;</pre></div></div><p></p><p> 我已经尝试过这些对我不起作用:</p><p> <a href="https://stackoverflow.com/questions/35646154/on-click-function-is-not-working-on-safari-browser" rel="nofollow noreferrer">单击 function 在 safari 浏览器上不起作用</a></p><p> <a href="https://stackoverflow.com/questions/6682302/onclick-not-working-in-safari" rel="nofollow noreferrer">onclick() 在 safari 中不起作用</a></p><p> <a href="https://stackoverflow.com/questions/21555241/jquery-click-not-working-on-ipad-iphone" rel="nofollow noreferrer">jquery 点击在 ipad/iphone 上不起作用</a></p><p>谢谢您的帮助!!</p></div> - Click event on <span> or <div> tag not working safari issue

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从父 div 的子级禁用单击事件 - Disabling a click event from the child of a parent div 如何从span标签上的click事件中获取选定的列表项值? - How do I get a selected list items value from a click event on a span tag? 如何获取div child中span标签的内容? - How to get content of span tag in div child? 如何在innerHTML中获取click事件? - How can I get the click event inside an innerHTML? 子div点击事件触发父点击事件 - child div click event triggers parent click event 如何防止孩子点击事件冒泡给父母? - How can I prevent child click event bubbling to parent? 点击事件<span>或</span><div>标签不工作 safari 问题</div><div id="text_translate"><p>我的 iPhone(7 Plus) 上出现了Click事件问题。 点击方法不适用于 IOS 设备。 但在 IOS Chrome 上工作正常。</p><p> 这是代码: </p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $('.menuOpen').on('click', function() { console.log('Show Menu'); $('.menuBar').toggleClass('d-none'); });</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"&gt; &lt;span class="menuOpen"&gt;Menu&lt;/span&gt; &lt;ul class="d-none menuBar"&gt; &lt;li&gt;Action 1&lt;/li&gt; &lt;li&gt;Action 2&lt;/li&gt; &lt;li&gt;Action 3&lt;/li&gt; &lt;li&gt;Action 4&lt;/li&gt; &lt;/ul&gt;</pre></div></div><p></p><p> 我已经尝试过这些对我不起作用:</p><p> <a href="https://stackoverflow.com/questions/35646154/on-click-function-is-not-working-on-safari-browser" rel="nofollow noreferrer">单击 function 在 safari 浏览器上不起作用</a></p><p> <a href="https://stackoverflow.com/questions/6682302/onclick-not-working-in-safari" rel="nofollow noreferrer">onclick() 在 safari 中不起作用</a></p><p> <a href="https://stackoverflow.com/questions/21555241/jquery-click-not-working-on-ipad-iphone" rel="nofollow noreferrer">jquery 点击在 ipad/iphone 上不起作用</a></p><p>谢谢您的帮助!!</p></div> - Click event on <span> or <div> tag not working safari issue innerHTML 生成的 div 上的点击事件 - click event on innerHTML generated div 如何在React中将点击事件从父元素传递给子元素? [ C ] - How can I pass a click event from a parent to child elements in React? [ c ] 如何 position 子 div 从父 div 的开头跨越到结尾? - How to position a child div to span from the beginning to the end of a parent div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM