[英]Uncaught syntax error: Adding <li> elements to an <ul>
I keep getting an uncaught syntex error
which I know usually means your code has a missing closing something. 我不断收到
uncaught syntex error
,我通常知道这意味着您的代码缺少关闭内容。 I keep failing to see what it is that I am missing. 我一直看不到我想念的是什么。
The idea of the function is that it extracts the a links ID and Text content and add's it to an un-ordered list. 该函数的思想是提取链接ID和文本内容,并将其添加到无序列表中。
The links have a class of 'ingredient_add' and the unordered list has an ID of 'ingredientsAdded'. 链接的类别为“ ingredient_add”,而无序列表的ID为“ingredient⚓”。
I can't see what I've missed here. 我看不到我在这里错过的一切。
$(document).ready(function() {
$('.ingredient_add').click(function() {
event.preventDefault();
var id = this.id;
var value = this.text();
$('#ingredientsAdded').append("<li id='"+id+"'>"+value+"</li>");
}); //end add to list
}); // end document ready()
Your syntax looks good. 您的语法看起来不错。 You do need to pass in the event to the function though.
您确实需要将事件传递给函数。
$('.ingredient_add').click(function(event){
event.preventDefault();
Check out Jonathan Lonowskis comment on your .text(). 查看Jonathan Lonowskis对您的.text()的评论。
The error you should be getting is Uncaught TypeError: undefined is not a function
您应该得到的错误是
Uncaught TypeError: undefined is not a function
$('.ingredient_add').click(function () {
event.preventDefault(); <-- what is event?
should be 应该
$('.ingredient_add').click(function (event) {
event.preventDefault();
if you are still getting that error, there is something else going on that is not in your code. 如果仍然出现该错误,则说明代码中没有发生其他情况。
Hello your problem was just a simple mistake. 您好,您的问题只是一个简单的错误。
var value = $(this).text();
Here is a jsfiddle: http://jsfiddle.net/Grimbode/pFLXf/1/ 这是一个jsfiddle: http : //jsfiddle.net/Grimbode/pFLXf/1/
I updated your code. 我更新了您的代码。 Watch out not to use the same id more than once.
注意不要多次使用相同的ID。
$(document).ready(function(){
var counter = 0;
$('.ingredient_add').click(function(event){
event.preventDefault();
var id = this.id;
var value = $(this).text();
$('#ingredientsAdded').append('<li id="'+id+counter+'">'+value+'</li>');
counter+=1;
}); //end add to list
}); // end document ready()
You missed an argument in click handler function. 您错过了点击处理程序函数中的参数。
$(document).ready(function(){
$('.ingredient_add').click(function(event){
event.preventDefault();
var id = this.id;
var value = this.text();
$('#ingredientsAdded').append("<li id='"+id+"'>"+value+"</li>");
}); //end add to list
}); // end document ready()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.