这是我面临的问题 - 我有一个名为 ('myDiv') 的 div。我设置了一个 setInterval 函数,它每 2 秒运行一次。我想要的是每次执行 setInterval 时,我都想额外旋转 div '15deg'。
我几乎做到了。但是当我进行一些字符串操作时,它出错了。 //这里是代码。
//css transform required value = 15ddeg not '15deg' 那么如何将'15deg'提取到15deg(没有qoute)
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-git2.js"></script>
<meta charset=utf-8 />
<script>
$(function(){
var foo = setInterval('animateDiv()', 2000);
var count = 15;
function animateDiv(){
//console.log(count);
count += 10;
$('.myDiv').css({'-webkit-transform':'rotate(count + "deg")'});
//console.log(count + "deg");
}
});
</script>
<title>JS Bin</title>
</head>
<body>
<div class='myDiv'></div>
</body>
</html>
最佳答案
(function(){
var intId = setInterval(animateDiv, 2000),
count = 15,
$myDiv = $('.myDiv'); //Cache the div so we don't keep looking though the DOM for the same element.
function animateDiv(){
console.log(count);
count += 10;
$myDiv.css({'-webkit-transform':'rotate(' + count +'deg)'});
//console.log(count + "deg");
}
}());
关于jquery - 从字符串中提取内部内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18199036/