在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)
如果只执行一个条件,为什么这会是一个坏习惯,这意味着变量只会被声明一次.它的真正问题是什么?
我目前正在使用此脚本来防止右键单击我的网站之一。我对代码进行了不同的调整,因为我希望它能够防止仅右键单击图像(右键单击它无处不在)。
任何想法?
在此先感谢您的帮助
//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 中切换元素的类? ”,但这个问题指的是属性。
我有一个像这样的复选框:
<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属性?
辅助功能问题:<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,或者它是否是一个实际错误。
我需要将格式化的数字转换为 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)
提前致谢。
尝试重新格式化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)我正在写一份注册表并尝试进行一些验证。当我的验证按钮位于内部时,我遇到了同样的问题,<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)我想,如果我选择"哺乳动物",动物选择选项只显示值为1的选项data-animal_class.
我知道如何获得哺乳动物的价值,但我对如何使用过滤器感到困惑
这是我的代码:
$('#class').on('change', function(e) {
e.preventDefault();
var animal = $(this).val();
console.log(animal);
})Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="class">
<option value=""></option>
<option value="1">Mammals</option>
<option value="2">Birds</option>
<option value="3">Reptiles</option>
</select>
<select id="animals">
<option value=""></option>
<option value="11" data-animal_class="1">Cow</option>
<option value="12" data-animal_class="1">Sheep</option>
<option value="13" data-animal_class="1">Bear</option>
<option value="21" data-animal_class="2">Parrot</option>
<option value="22" data-animal_class="2">Pigeons</option>
<option value="23" data-animal_class="2">Bear</option>
<option value="31" data-animal_class="3">Crocodile</option>
<option value="32" data-animal_class="3">Lizard</option>
<option value="33" data-animal_class="3">Turtle</option>
</select>Run Code Online (Sandbox Code Playgroud)
是否有可能在每次单击按钮/图标(除了第一个带有标题的按钮/图标)时打乱表格行并使它们随机出现?类似于 w3schools 的“如何对表进行排序”(https://www.w3schools.com/howto/howto_js_sort_table.asp),但表会随机排序。
另一种可能性是使用 JavaScript 数组排序,但我不知道如何使表行显示为数组的内容。
https://jsfiddle.net/17bjxgfa/1/
我更喜欢普通的 JS 解决方案而不是 jQuery。
这是我们可以使用的示例表:
.table-div {
padding-top: 1rem;
}Run Code Online (Sandbox Code Playgroud)
<div class="table-div">
<table id="myTable">
<tr>
<th class="button"><button class="my-btn" type="button" onclick="sortTable()">
shuffle</button></th>
<th>Text:</th>
<th></th>
</tr>
<tr>
<td class="left">Some text 1</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 2</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 3</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td> …Run Code Online (Sandbox Code Playgroud)我试图将一个类添加到无序列表的第二个列表元素中,但是即使我可以将相同的类添加到第一个列表元素中,该类也无法正常工作,有人可以告诉我我哪里出错了,在此先感谢!
var url = "http://myshop.com/about-us/"; // window.location.href;
if (url == 'https://myshop.com/') {
$("ul.menu.low li a:first").addClass("active white");
}
if (url.search("about-us") >= 0) {
$("ul.menu.low li a:nth-child(2)").addClass("active red");
}Run Code Online (Sandbox Code Playgroud)
a.active.red {
border: 2px solid red;
max-width: 70px;
color: red !important;
}
a.active.white {
border: 2px solid #fff;
max-width: 70px;
color: #fff !important;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="menu low">
<li><a href="http://myshop.com/" target="_self">Home</a></li>
<li> <a href="http://myshop.com/about-us/" target="_self">About</a>
<ul>
<li><a href="http://myshop.com/hi" target="_self"><span>Hi</span></a></li>
<li><a href="http://myshop.com/bye" target="_self"><span>Bye</span></a></li>
</ul>
</li>
</ul>Run Code Online (Sandbox Code Playgroud)
javascript ×7
html ×6
jquery ×4
css ×2
checkbox ×1
forms ×1
material-ui ×1
toggle ×1
togglebutton ×1