簡體   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>

告訴我是否可以改善此答案。

請感謝@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> 

這是一個通用的解決方案,可用於所需的多個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 的子級禁用單擊事件 如何從span標簽上的click事件中獲取選定的列表項值? 如何獲取div child中span標簽的內容? 如何在innerHTML中獲取click事件? 子div點擊事件觸發父點擊事件 如何防止孩子點擊事件冒泡給父母? 點擊事件<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> innerHTML 生成的 div 上的點擊事件 如何在React中將點擊事件從父元素傳遞給子元素? [ C ] 如何 position 子 div 從父 div 的開頭跨越到結尾?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM