无法添加两个字符串(+=操作)
Can not add two String ( += operation )
我正在做一个项目,让用户鼠标滚轮移动,并知道它向上或向下滚动。在我的代码中,我可以上下移动。但我想把动作保存到字符串中。例如,如果用户向上向上向下滚动"mhmh"显示返回"UUD",但现在它只得到最后一个移动(只有U o D(
<script type="text/javascript">
function displaywheel(e){
var evt=window.event || e //equalize event object
var delta=evt.detail? evt.detail*(-120) : evt.wheelDelta
//check for detail first so Opera uses that instead of wheelDelta
var mhmh = ''
var t = ''
//mhmh = (delta<=-120)? mhmh + 'D' : mhmh + 'U'
if (delta<=-120)
t = "d"
else
t = "u"
mhmh = mhmh + t
document.getElementById("wheelvalue").innerHTML= mhmh //delta returns +120 when wheel is scrolled up, -120 when down
}
var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x
if (document.attachEvent) //if IE (and Opera depending on usesr setting)
document.attachEvent("on"+mousewheelevt, displaywheel)
else if (document.addEventListener) //WC3 browsers
document.addEventListener(mousewheelevt, displaywheel, false)
</script>
你可以在这里浏览网页样本。怎么了?
问题是每次调用事件处理程序时都要重新定义mhmh
。这就是闭包派上用场的地方。将mhmh
变量存储在事件处理程序上下文之外:
var mhmh = '';
function displaywheel(e){
var evt=window.event || e //equalize event object
var delta=evt.detail? evt.detail*(-120) : evt.wheelDelta
//check for detail first so Opera uses that instead of wheelDelta
var t = ''
//mhmh = (delta<=-120)? mhmh + 'D' : mhmh + 'U'
if (delta<=-120)
t = "d"
else
t = "u"
mhmh = mhmh + t
document.getElementById("wheelvalue").innerHTML= mhmh //delta returns +120 when wheel is scrolled up, -120 when down
}
var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x
if (document.attachEvent) //if IE (and Opera depending on usesr setting)
document.attachEvent("on"+mousewheelevt, displaywheel)
else if (document.addEventListener) //WC3 browsers
document.addEventListener(mousewheelevt, displaywheel, false)
只需将变量mhmh作为全局变量
`
var mhmh = '';
function displaywheel(e){
var evt=window.event || e //equalize event object
var delta=evt.detail? evt.detail*(-120) : evt.wheelDelta
//check for detail first so Opera uses that instead of wheelDelta
var t = ''
//mhmh = (delta<=-120)? mhmh + 'D' : mhmh + 'U'
if (delta<=-120)
t = "d"
else
t = "u"
mhmh = mhmh + t
document.getElementById("wheelvalue").innerHTML= mhmh //delta returns +120 when wheel is scrolled up, -120 when down
}
var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x
if (document.attachEvent) //if IE (and Opera depending on usesr setting)
document.attachEvent("on"+mousewheelevt, displaywheel)
else if (document.addEventListener) //WC3 browsers
document.addEventListener(mousewheelevt, displaywheel, false)
`
您的变量"mhmh"在函数的本地作用域中,它总是被重置。将其设置为函数外的全局变量,然后可以连接鼠标移动并在需要时重置变量。
相关文章:
- 是否可以像字符串一样/操作/函数
- 如何将生成的字符串放入Form操作和输入中
- JavaScript 相等操作对单引号字符串和数字都成功
- Javascript 返回 OR(||) 操作的字符串
- JavaScript,字符串/对象操作的例外
- 在不更改 HTML 的情况下操作 HTML 字符串的内容
- 操作的Javascript字符串验证
- 如何从JavaScript中的HTML字符串中操作DOM
- 试图执行JSON.parse,但它不喜欢我使用服务器返回的字符串所做的操作
- 为什么在Redux存储中使用字符串而不是直接方法调用来触发操作
- 如何在javascript中进行字符串操作
- 表单操作查询字符串中需要下拉选项值
- 如何在javascript中操作字符串
- 只需少量操作即可获得子字符串
- Javascript-如何删除字符串的一部分并将其分配给变量进行操作?字符串是可变的
- 在javascript中操作字符串
- 我如何在JSX中操作字符串
- 使用lodash在JavaScript数组中操作字符串
- 通过操作字符串将时间转换为 12 小时
- 在操作字符串时,获得Javascript长时间运行脚本错误的机会是什么?