[英]Ajax in a wordpress page
I am trying to use the following piece of ajax jquery in a WordPress page: 我正在尝试在WordPress页面中使用以下ajax jquery:
1. <script>
2. $(document).ready(function(){
3. $("button").click(function(){
4. $.ajax({
5. method: 'GET',
6. url: "http:Sample.url.com",}).done(function (data)
7. {
8. var f_table= "";
9. for (var i = 0; i < data.unittopics.topics.length; i++) {
10. f_table+= "<tr><td>" +data.unittopics.topics[i].name+ "</td><td>" +data.unittopics.topics[i].description+ "</td><td>" + "<a id=" +'Frac_Link'+i+ " href='#'>" +'Click here'+ "</td></tr>";
11. alert("#div"+data.unittopics.topics[i].id);
}
12. $("#Frac_Table").append(f_table);
13. for (var x = 0; x < data.unittopics.topics.length; x++)
{
14. $("#Frac_Link"+x).attr('href', data.unittopics.topics[x].externalUrl);
alert("working");
}
});
});
});
</script>
I have tested this a jquery in w3school testing environment and it work perfect, but when I put it into WordPress it throws the following error Invalid or unexpected token
on row 10. is there a simple way to fix this? 我已经在w3school测试环境中测试了这个jQuery,它可以完美工作,但是当我将它放入WordPress时,它在第10行上抛出了以下错误Invalid or unexpected token
。有没有简单的方法来解决此问题?
Thanks. 谢谢。
我遇到了同样的问题,我将$更改为对我有用的jQuery
Wordpress runs with jQuery in no-conflict mode, so replaces the use of $
with jQuery
, so either, replace all instances of $
with jQuery
, or... WordPress在无冲突模式下与jQuery一起运行,因此将$
的使用替换为jQuery
,因此可以将$
所有实例替换$
jQuery
,或者...
var $ = jQuery;
That should fix it. 那应该解决它。
All the above are correct and solved another problem I didn't know I had. 以上所有都是正确的,并解决了我不知道的另一个问题。 But the problem I posted about was resolved my separating the <tr><td>
tags in my jQuery. 但是我发布的问题已解决,我在jQuery中分离了<tr><td>
标签。
solution 解
jQuery(document).ready(function(){
jQuery("#frac_btn").one('click', function() {
jQuery.ajax({
method: 'GET',
url: 'http:Sample.url.com'
}).done(
function (data) {
var f_table= "";
for (var i = 0; i < data.unittopics.topics.length; i++) {
f_table += '<tr' + '><td' + '>' +
data.unittopics.topics[i].name + '</td' + '><td' + '>' +
data.unittopics.topics[i].description + '</td' + '><td' + '>' +
'<a id=' + 'Frac_Link' + i + " href='#'>" + 'Click here' +
'</td' + '></tr' + '>';
}
jQuery("#Frac_Table").append(f_table);
for (var x = 0; x < data.unittopics.topics.length; x++) {
jQuery('#Frac_Link' + x).attr('href', data.unittopics.topics[x].externalUrl);
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.