My Text Javascript代码-6ren">
gpt4 book ai didi

javascript - 在带有 Ajax 的 javascript 中使用 'this'

转载 作者:行者123 更新时间:2023-12-03 07:18:13 26 4
gpt4 key购买 nike

我从 Ajax 调用这个 php 文件,并在此 php 文件中编写所需的 javascript 代码。

<div class="add_people_invite">
<button class="btn btn-default add_connection" id="right_connection_request" onclick="SentRequestMouseUpRight(event,{{ $peopleMayKnow->id }})">
<span>My Text</span>
</button>
</div>

Javascript代码:

<script>
function SentRequestMouseUpRight(event, user_id) {
if (event.which == 1) {
alert();
$("#right_connection_request span", this).text("My NEW Text");
}
}
</script>

javascript 中的警报正在工作,但它不会将文本更改为“我的新文本”,如果我删除 this,然后运行此代码 $("#right_connection_request span").text (“我的新文本”);它改变了文本。

最佳答案

您可以使用event.target代替this关键字。 Here是一个 fiddle 来演示相同的内容。

function SentRequestMouseUpRight(event, user_id) {
if (event.which == 1) {
$(event.target).text("My NEW Text");
}
}

关于javascript - 在带有 Ajax 的 javascript 中使用 'this',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36317221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com