简体   繁体   English

iScroll 4脚本-如何减少体积?

[英]iScroll 4 script - how can I make this less bulky?


I'm very new to javascript, and I'm not sure what to do with my script below. 我对javascript很陌生,我不确定如何处理下面的脚本。

Please see script below which is a resource I found to help me activate input's within iscroll4... 请查看下面的脚本,该脚本是我发现的可帮助我在iscroll4内激活输入的资源...

var inputSubmit = document.getElementById('gform_submit_button_1');

inputSubmit.addEventListener('touchstart' /*'mousedown'*/, function(e) {
    e.stopPropagation();
}, false);



This is great for one input, but I've got an entire form to apply this too. 这对于一次输入非常有用,但是我也有完整的表格可以应用。 So this is how I wrote it... 所以这就是我写的方式

var inputSubmit = document.getElementById('gform_submit_button_1'),
    inputName = document.getElementById('input_1_1'),
    inputEmail = document.getElementById('input_1_2'),
    inputPhone = document.getElementById('input_1_3'),
    inputMessage = document.getElementById('input_1_4'),
    inputCaptcha = document.getElementById('input_input_1_5');

inputSubmit.addEventListener('touchstart' /*'mousedown'*/, function(e) {
    e.stopPropagation();
}, false);

inputName.addEventListener('touchstart' /*'mousedown'*/, function(e) {
    e.stopPropagation();
}, false);

inputEmail.addEventListener('touchstart' /*'mousedown'*/, function(e) {
    e.stopPropagation();
}, false);

inputPhone.addEventListener('touchstart' /*'mousedown'*/, function(e) {
    e.stopPropagation();
}, false);

inputMessage.addEventListener('touchstart' /*'mousedown'*/, function(e) {
    e.stopPropagation();
}, false);

inputCaptcha.addEventListener('touchstart' /*'mousedown'*/, function(e) {
    e.stopPropagation();
}, false);


The variables are probably fine as the are, but is there some way I can have less script and combine the bottom part into one? 变量可能很好,但是有什么方法可以减少脚本并将底部合并为一个?

If this is how it's got to be then no worries. 如果这是必须的,那就不用担心了。

Any advice would be really helpful. 任何建议都会非常有帮助。


Thanks 谢谢

var formInputs = [
  document.getElementById('gform_submit_button_1'),
  document.getElementById('input_1_1'),
  document.getElementById('input_1_2'),
  document.getElementById('input_1_3'),
  document.getElementById('input_1_4'),
  document.getElementById('input_input_1_5')
];
for(var i = 0; i < formInputs.length; i++) {
  formInputs[i].addEventListener('touchstart' /*'mousedown'*/, function(e) {
    e.stopPropagation();
  }, false);
}

Or you can loop it by 或者你可以通过循环

formInputs.forEach(function(el) {
  el.addEventListener('touchstart' /*'mousedown'*/, function(e) {
    e.stopPropagation();
  }, false);
});

Or.. (Not recommended because of: Why is using "for...in" with array iteration a bad idea? ) 或。(不建议使用,因为: 为什么在数组迭代中使用“ for ... in”是个坏主意?

for(var el in formInputs) {
  el.addEventListener('touchstart' /*'mousedown'*/, function(e) {
    e.stopPropagation();
  }, false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM