首页 新闻 会员 周边

js实现标题跑马灯功能,右移不能正常实现,没找到原因。。。

0
悬赏园豆:5 [已解决问题] 解决于 2020-09-12 01:50

代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>ABCDEFGHIJKL</title>
<script type="text/javascript">
var direction = 'left';
onload = function () {
setInterval(function () {
var titleText, firstChar, others;
titleText = document.title;
if (direction = 'left') {
firstChar = titleText.charAt(0);
others = titleText.substring(1);
document.title = others + firstChar;
} else {
firstChar = titleText.charAt(title.length - 1);
others = titleText.substring(0, title.length - 1);
document.title = firstChar + others;
}
}, 200);
document.getElementById('btnr').onclick = function () {
direction = 'right';
};
document.getElementById('btnl').onclick = function(){
direction = 'left';
};
};
</script>
</head>
<body>
<input type="button" id="btnl" value="←" />
<input type="button" id="btnr" value="→" />
</body>
</html>

问题补充:

意识源码的主页 意识源码 | 初学一级 | 园豆:197
提问于:2020-09-11 19:20
< >
分享
最佳答案
2

在 js 里面判断的时候要用两个或者三个等号
一个等号是赋值的意思
另外你有一个未定义的变量 title

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>ABCDEFGHIJKL</title>
<script type="text/javascript">
var direction = 'left';
onload = function () {
setInterval(function () {
var titleText, firstChar, others;
titleText = document.title;
if (direction === 'left') {
firstChar = titleText.charAt(0);
others = titleText.substring(1);
document.title = others + firstChar;
} else {
firstChar = titleText.charAt(titleText.length - 1);
others = titleText.substring(0, titleText.length - 1);
document.title = firstChar + others;
}
}, 200);
document.getElementById('btnr').onclick = function () {
direction = 'right';
};
document.getElementById('btnl').onclick = function(){
direction = 'left';
};
};
</script>
</head>
<body>
<input type="button" id="btnl" value="←" />
<input type="button" id="btnr" value="→" />
</body>
</html>
收获园豆:5
by.Genesis | 老鸟四级 |园豆:2719 | 2020-09-11 23:54

😓晕๑_๑这种低级错误,害我找半天没发现if条件判断里少了个=,title是标签

意识源码 | 园豆:197 (初学一级) | 2020-09-12 00:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册