英文:
Uncaught SyntaxError: Unexpected token '-'
问题
<script type="text/javascript">
jQuery('.mit div')
.attr({"data-slick": '{"slidesToShow": 3, "slidesToScroll": 1}' });
</script>
需要在使用 slick slide 时得到以下结果:
<div data-slick='{"slidesToShow": 4, "slidesToScroll": 4}'>
英文:
I'm trying to add some attribute to an element with some jQuery, but I'm getting a syntax error Uncaught SyntaxError: Unexpected token '-'
<script type="text/javascript">
jQuery('.mit div')
.attr({data-slick:'{"slidesToShow": 3, "slidesToScroll": 1}'
});
</script>
I need to have a result as this below when working with slick slide.
<div data-slick='{"slidesToShow": 4, "slidesToScroll": 4}'>
答案1
得分: 7
你不能在JavaScript对象键中使用-
。如果需要使用-
,则必须使用"
或'
进行转义:
jQuery('.mit div')
.attr({'data-slick':'{"slidesToShow": 3, "slidesToScroll": 1}'});
英文:
You can't have a -
in a JavaScript object key. If you need one, then you have to escape it with "
or '
:
jQuery('.mit div')
.attr({'data-slick':'{"slidesToShow": 3, "slidesToScroll": 1}'
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论