小编mpl*_*jan的帖子

迭代时出错 - 意外令牌

我正在尝试迭代获取数据属性并将其存储到变量,但我得到了

未捕获的SyntaxError:意外的令牌{

const monthButton = document.querySelector('.month'),
        annualButton = document.querySelector('.annualy'),
        price = document.querySelectorAll('.price');


monthButton.addEventListener('click', function{
  for(var i=0; i>=4; i++){
    const dataMonth[i] = price[i].dataset.monthly;
  }
})
Run Code Online (Sandbox Code Playgroud)

javascript custom-data-attribute

1
推荐指数
1
解决办法
468
查看次数

在if条件中声明变量两次可能会出现什么问题?

在if条件中声明变量两次可能会出现什么问题?

我知道这不是最好的方法!
我知道我可以在if条件之外声明变量.

我不是在寻求解决方案!我不知道如何声明我的变量.我想理解为什么这是一个在if条件块中声明变量的错误方法.

/* Yes I know I can just have condition instead of condition === true, 
   this is only for simplicity */
if(condition === true){ 
    var StuckUps = "over 9000";
}else if(condition === false){
    var StuckUps = "Nothing";
}

alert(StuckUps) /* Yes I can access it outside the if condition!*/
Run Code Online (Sandbox Code Playgroud)

如果只执行一个条件,为什么这会是一个坏习惯,这意味着变量只会被声明一次.它的真正问题是什么?

javascript

1
推荐指数
1
解决办法
70
查看次数

禁用图像上的右键单击

我目前正在使用此脚本来防止右键单击我的网站之一。我对代码进行了不同的调整,因为我希望它能够防止仅右键单击图像(右键单击它无处不在)。

任何想法?

在此先感谢您的帮助

//Disable right mouse click Script

var message = "Stop trying stealing pics! :P";

///////////////////////////////////
function clickIE4() {
  if (event.button == 2) {
    alert(message);
    return false;
  }
}

function clickNS4(e) {
  if (document.layers || document.getElementById && !document.all) {
    if (e.which == 2 || e.which == 3) {
      alert(message);
      return false;
    }
  }
}

if (document.layers) {
  document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown = clickNS4;
} else if (document.all && !document.getElementById) {
  document.onmousedown = clickIE4;
}

document.oncontextmenu = new Function("alert(message);return false")
Run Code Online (Sandbox Code Playgroud)

javascript jquery

1
推荐指数
1
解决办法
6571
查看次数

无需 jQuery 即可切换复选框选中属性

我见过类似的问题,例如“如何在纯 JavaScript 中切换元素的类? ”,但这个问题指的是属性。

我有一个像这样的复选框:

<span class="newsletter-checkbox">
  <input type="checkbox" name="newsletter" id="newsletter" checked> Sign up to the newsletter as well - recieve new posts in your inbox
</span>
Run Code Online (Sandbox Code Playgroud)

如何做到这样,当.newsletter-checkbox单击其中的任何位置(包括文本)时,它会切换复选框checked属性?

html javascript checkbox toggle togglebutton

1
推荐指数
1
解决办法
6323
查看次数

可访问性:标签除了输入之外还可以有子项吗?

辅助功能问题:<label>...</label>除了 a 之外,a 还可以有孩子<input ... />吗?我们在一个项目中使用 Material UI,其<Switch />组件的构建方式如下:

<label>
  <div>
    <span>
      <span>
        <input />
      </span>
    </span>
  </div>
</label>
Run Code Online (Sandbox Code Playgroud)

这会在 HTML_Codesniffer 中触发可访问性错误。

不确定它是否只是不习惯此结构并期望它label是其直接父级input,或者它是否是一个实际错误。

html accessibility material-ui

1
推荐指数
1
解决办法
1668
查看次数

如何恢复到LocaleString

我需要将格式化的数字转换为 JS 默认数字格式。

这是我的代码:

String.prototype.toJsFloatFormat = function() {
          debugger;
          var newVal = this;
          return newVal;
        }
//Example of use
var input = 10000.22; //default js format
var formatted = input.toLocaleString("es"); // result is: 10.000,22
var unformatted = formatted.toJsFloatFormat(); //expected result = 10000.22;
Run Code Online (Sandbox Code Playgroud)

问题是当我需要获取格式化数字 (10.000,22) 并使用此格式化数字 (parseFloat(10.000,22) + 1000) 进行操作时,我得到了不好的结果 ( parseFloat(10.000,22) + 1000 = 1010)

提前致谢。

javascript globalization jquery

1
推荐指数
1
解决办法
4044
查看次数

在Javascript中重新格式化JSON

尝试重新格式化API查询的响应,但遇到问题.尝试过地图但没有奏效.

main.data.daily(symbol, 'main', 'json').then(data=> ....);
Run Code Online (Sandbox Code Playgroud)

目前的回复格式:

'data':{
  '2018-03-13':
    { '1. open': '32.8500',
      '2. high': '33.3600',
      '3. low': '32.8500',
      '4. close': '33.1400',
      '5. volume': '834894' 
    },
    ...
 }
Run Code Online (Sandbox Code Playgroud)

这是所需的格式:

[{ 
 date: '2018-03-13'
 open: 32.85, 
 high: 33.36, 
 low: 33.85,
 close: 33.14, 
 volume: 855448
},
...
]
Run Code Online (Sandbox Code Playgroud)

试过以下但没有雪茄:

data.map(val, i, data => {
            return {
                date: i,
                open: val['1. open'],
                high: val['2. high'],
                low: val['3. low'],
                close: val['4. close'],
                volume: val['5. volume']
            }
        });
Run Code Online (Sandbox Code Playgroud)

var data = {
  '2018-03-13': {
    '1. open': '32.8500', …
Run Code Online (Sandbox Code Playgroud)

javascript

1
推荐指数
1
解决办法
47
查看次数

如何在提交后清空表单

大家好,这是一个有点复杂的形式,所以我设法在发送消息后发送消息警告消息,如何在发送后删除整个表单最简单的时间?

这是我的HTML表单和js代码,这里我需要在发送后清空所有字段!

$(document).ready(function() {
  $('#my-form').submit(function(e) {

    $.ajax({
      type: "POST",
      url: "handler.php",
      data: $("#my-form").serialize(),
      success: function(data) {
        alert("Vielen Dank für Ihre Anfragen, wie werden Sie so rasch wie möglich kontaktieren!");
      }
    })
    e.preventDefault();
  })
})
Run Code Online (Sandbox Code Playgroud)
<!-- Start Contact Form -->
<div class="triangle"></div>
<div id="cform" class="container">
  <div class="row">
    <h3 class="my-title contact-title">Kontaktformular</h3>
    <hr>
    <div class="col-md-12">
      <form id="my-form" method="post" action="handler.php">
        <ul class="contact-form">
          <li>
            <div class="col-md-6">
              <input name="name" placeholder="Name" required="required" size="8" type="text">
            </div>

            <div class="col-md-6">
              <input name="email" placeholder="E-Mail" required="required" size="8" type="email">
            </div>
          </li>
          <li>
            <div …
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery

1
推荐指数
1
解决办法
97
查看次数

Javascript:焦点不是函数

我正在写一份注册表并尝试进行一些验证。当我的验证按钮位于内部时,我遇到了同样的问题,<form>但是为了解决这个问题,我只是将其移出,因为我猜测该按钮导致表单刷新。然而现在在“ValidateForm()”函数中插入多个 if 语句后,此错误消息似乎又回来了。

错误:未捕获 TypeError:Forename.focus 不是
ValidateForm(登录表单 Complex.html?Forename=&Surname=&Username=&Password=&Email=:79)处的函数
,位于 HTMLButtonElement.onclick(登录表单 Complex.html?Forename=&Surname=&Username) =&密码=&电子邮件=:63)**

function ValidateForm() {
  var Forename = document.getElementById("Forename").value;
  var Surname = document.getElementById("Surname").value;
  var Username = document.getElementById("Username").value;
  var Password = document.getElementById("Password").value;
  var Email = document.getElementById("Email").value;
  var Errors = 0

  if (Forename == "") {
    document.LoginForm.Forename.focus();
    Forename.focus();
    Errors = Errors + 1
  }

  if (Surname == "") {
    document.LoginForm.Forename.focus();
    Surname.focus();
    Errors = Errors + 1

  }

  if (Username == "") {
    document.LoginForm.Forename.focus();
    Username.focus();
    Errors = Errors + 1

  } …
Run Code Online (Sandbox Code Playgroud)

html javascript css forms

1
推荐指数
1
解决办法
2万
查看次数

Concat两个数组,其中一个也是数组

如何连接两个数组,其中一个也是数组。看起来像这样

let stat = [
  ["completed", "0", "0", "0"],
  ["due", "1", "2", "2"],
  ["warning", "0", "0", "0"]
]

let val = [
  [0, 0, 0],
  [1, 2, 2],
  [0, 0, 0]
]



var arr = [];
for (var i = 0; i < stat.length; i++) {
  var temp = [stat[i][0].concat([val[i]])];
  arr.push(temp);
}
console.log(arr)
Run Code Online (Sandbox Code Playgroud)

当console.log时,它将打印出:

["Completed0,0,0"]
["Due1,2,2"]
["Warning0,0,0"]
Run Code Online (Sandbox Code Playgroud)

我希望结果看起来像这样:

["Completed", 0, 0, 0]
["Due", 1, 2, 2]
["Warning", 0, 0, 0]
Run Code Online (Sandbox Code Playgroud)

我如何获得这样的新阵列?谢谢!

javascript arrays concatenation

1
推荐指数
1
解决办法
117
查看次数