I am trying to use the following piece of ajax jquery in a WordPress page:
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?
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...
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.
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);
}
});
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.