I made a small function to calculate width along with margin of li but it is giving me error.
<head>
<style>
.bill ul{ list-style:none}
.bill ul li{ float:left; width:94px; margin-right:18px}
.bill ul li:last-child { margin-right:0}
</style>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function (){
$('.bill').find('li').each(function (){
var jn += parseInt($(this).css('margin-right')) + $(this).width();
})
alert(jn)
})
</script>
</head>
<body>
<div class="bill">
<ul>
<li>aaa</li>
<li>aaa</li>
<li>aaa</li>
<li>aaa</li>
<li>aaa</li>
</ul>
</div>
</body>
You’re declaring the
jnvariable using thevarkeyword inside the anonymous function passed to.each(), meaning that the variable will be scoped inside of that function and not exist outside of it.Trying to call
alert(jn)will likely result inundefinedbeing alerted, because thejnvariable isn’t visible in the scope of that statement.In addition, declaring a variable and using the
+=operator is invalid syntax. The variable has to exist in order for you to add to its value. Try the following: