這是我的HTML內容
<textarea>
<p>some text</p>
</textarea>
在我的代碼中,有很多這樣的textarea
。當textarea里面沒有任何HTML標簽時,我可以得到預期的textarea,但是當textarea里面有一個p標簽時,我的代碼只能得到p標簽而不能得到父標簽
body.click((evt) => //span> {
console.log($(evt.target)); // return the p tag
console.log($(evt.target) 。 parent()); // return undefined.
console.log($(evt.target) 。 parents('textarea')); // returned undefined.
console.log($(evt.target).nodeParent); //回傳undefined。
}
uj5u.com熱心網友回復:
你可以將jQuery .is()方法與e.target結合在一起,如下面的演示。請注意,在這個演示中,你必須在form中點擊才能觸發點擊事件。
$(function() {
const textarea = $('#bio') 。
$('form').on('click', function(e) {
console.log( e.target ) 。
if( $(e.target).is(textarea) ) {
console.log( 'You clicked inside the textarea' ) 。
} else {
console.log( 'You clicked outside the textarea' ) 。
}
});
});
<script src="https://cdnjs. cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>/span>
<form>
< input type="text" name="name" placeholder="Full Name"> <br><br>
< input type="email" name="email" placeholder="Email"> <br><br>
< textarea name="bio" id="bio" placeholder="BIO"> </textarea>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
也許你應該給你的textarea一個id,并在它上面系結點擊事件。
const textarea = document. getElementById('myTextarea')。
textarea.onclick = (ev) => console.log(ev) 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/306720.html
標籤:
