如何解决JavaScript错误:"$ is not a function"



我有一段javascript在Wordpress网站上引发错误:

<script type="text/javascript">
$.ajax({
url: 'https://api.xxxxx.xxx/v1/identify',
success: function(data){
//show data in your browser console
console.log(data);
//example on how to replace an element in your page
if(data.hasOwnProperty('name')) {
$('.injected-header-text').html('Hi ' + data.name);
}
}
});

```

在浏览了几个线程后,我将$.ajax更改为jQuery.ajax,这使得请求的结果在Chrome控制台中可见,但在浏览器中不可见。

错误:JavaScript错误:"$不是函数";

这就是我目前所拥有的:

<script>
jQuery.ajax({url: 'https://api.xxxx.xxx/v1/identify',
success: function(data){
//show data in your browser console
console.log(data);
//example on how to replace an element in your page
if(data.hasOwnProperty('name')) {
$('.injected-header-text').html('Welkom ' + data.name);
}
}
});
</script>
<div class=injected-header-text > </div>

在顶部添加javascript/jQuery cdn

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

相关内容

最新更新