我从 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/