最近,我读过TJ的博客文章:"Farewell Node.js".
我不太了解Node失败的部分.这里是:
在我看来,Go中的错误处理是优越的.节点很棒,因为你必须考虑每个错误,并决定做什么.然而节点失败是因为:
- 你可能会得到重复的回调
- 你可能根本没有得到回调(迷失方向)
- 你可能会得到带外错误
- 发射器可能会发生多个"错误"事件
- 缺少"错误"事件将一切都发送到地狱
- 经常不确定什么需要"错误"处理程序
- "错误"处理程序非常冗长
- 回调很糟糕
当作者写道"你可能根本没有得到回调(陷入困境)"时,会提到什么具体问题?
尝试运行时,我得到"未定义不是函数".我错过了什么?
function bench(func) {
var start = new Date.getTime();
for(var i = 0; i < 10000; i++) {
func();
}
console.log(func, new Date.getTime() - start);
}
function forLoop() {
var counter = 0;
for(var i = 0; i < 10; i++) {
counter += 1;
}
return counter;
}
bench(forLoop);
Run Code Online (Sandbox Code Playgroud) 我的代码似乎没有用.我试图访问div的第一个子并设置属性,但控制台给我这个错误:
TypeError: Object # has no method 'setAttribute'
我的代码:
<div>
<p id="one"><span id="uno">1</span></p>
<p id="two"><span id="dos">2</span></p>
<p id="three"><span id="tres">3</span></p>
</div>
<script>
var divvy = document.getElementsByTagName("div").item(0)
divvy.firstChild.setAttribute("style", "color: violet;");
</script>
Run Code Online (Sandbox Code Playgroud) 我想知道当变量需要在同一模块中的多个函数中使用时,防止变量为全局的最佳方法是什么.
正如你所看到的那样,我需要在整个过程中提供这些变量 - 但是我并不一定希望在全局中定义这些变量,因为最终会把事情搞得一团糟.我将它们包装在一个函数中吗?
此外,如果有人提出改进此代码的方法,我很乐意听到它.
这是我的代码示例:
// Info Bullet Slide Out
var slideOut,
clickedButton,
clickedParent,
activeClass = 'is-active',
openClass = 'is-open';
function closeSlideOut(){
$('.tlx-img-point').removeClass(activeClass);
slideOut.removeClass(openClass);
clickedParent.removeClass(activeClass);
}
function openSlideOut(){
slideOut = $('.' + clickedButton.attr('id'));
slideOut.addClass(openClass);
clickedParent.addClass(activeClass);
clickedButton.addClass(activeClass);
}
$('.tlx-img-point').on('click', function(){
clickedButton = $(this);
clickedParent = clickedButton.parent();
// If you clicked on the same button twice just close the slideout
if($(this).hasClass('is-active')){
closeSlideOut();
// If you clicked on another info button close this one and open the new one
}else if(clickedParent.hasClass(activeClass)){
closeSlideOut(); …Run Code Online (Sandbox Code Playgroud) 我有这个表如下所示...我如何只根据changeno选择id的最新数据?
+----+--------------+------------+--------+
| id | data | changeno | |
+----+--------------+------------+--------+
| 1 | Yes | 1 | |
| 2 | Yes | 2 | |
| 2 | Maybe | 3 | |
| 3 | Yes | 4 | |
| 3 | Yes | 5 | |
| 3 | No | 6 | |
| 4 | No | 7 | |
| 5 | Maybe | 8 | |
| 5 | Yes | …Run Code Online (Sandbox Code Playgroud) 我是JavaScript和CSS的新手,在找出我应该使用的"getelementsby"以及我应该使用的CSS类型方面遇到了一些麻烦.
我试图用JavaScript调用的CSS属性是:
.restore a:link, .restore a:visited
Run Code Online (Sandbox Code Playgroud)
在我的网页上,链接如下所示:
<a target="_blank" href="/coolnewstuff.html">
Run Code Online (Sandbox Code Playgroud)
我正在使用的JavaScript是:
<script>
function replaceLinks() {
var links = document.getElementsByTagName('.restore a:link');
for (var i = 0; i < links.length; i++) {
links[i].innerHTML = 'DOWNLOAD register here.';
links[i].href = 'register.php';
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
但它不起作用(我认为因为getelementsby是错误的.
我尝试更改.restore a:link为just 'a'但它替换了我页面上的每个链接,我只希望它替换我的帖子内容中的链接,只有链接(而不是链接到其全尺寸图像的图像).
任何帮助是极大的赞赏.
我正在编写一个小函数,它接受一个参数并尝试调用parseInt(value) || value.toUpperCase()但是它无法按预期使用某些值.
function convert(value)
{
return parseInt(value) || value.toUpperCase();
}
Run Code Online (Sandbox Code Playgroud)
一些示例输出是
convert("asdf") -> "ASDF"
convert("Car") -> "CAR"
convert("1") -> 1
convert("52") -> 52
但由于某种原因,当我输入"0"时,我得到"0"退出.我试图调用parseInt("0")并且它正确地解析0出来,但是当|| "0".toUpperCase()它与它结合时总是返回字符串"0".
唯一的借口,我能想出的是,0 || "0"总是要解决的"0",因为它处理0为未定义或为空(这我的理解是,短路计算的JavaScript才被短路undefined,null或false值).
我希望有人可以在这个问题上给我一些清晰的认识.
当您拥有对象时,如何使用 JavaScript获取form元素。idform
使用form.id将是一件好事,但如果form有一个<input id="id">返回该input对象,而不是id对的form元素。
在python 2.7中,以下代码返回正确的结果(-18027917)
from __future__ import print_function
def twos_comp(val, bits):
if (val & (1 << (bits - 1))) != 0:
val -= 1 << bits
return val
valBinary = "110111011001110101001110011"
print(twos_comp(int(valBinary, 2), len(valBinary)))
Run Code Online (Sandbox Code Playgroud)
在JavaScript(Node.js)中,以下代码返回不正确的结果(1995238003)
function toTwosComplement(val, bits) {
if ((val & (1 << (bits - 1))) != 0) {
val -= (val - 1) << bits;
}
return val;
}
valBinary = "110111011001110101001110011"; // same as python example
console.log(toTwosComplement(parseInt(valBinary, 2), valBinary.length));
Run Code Online (Sandbox Code Playgroud)
显然,位运算符(或int/parseInt)的行为有所不同,但我无法看到它是什么.
在Jquery中,如果要选择某些内容,可以使用各种选择器组合.有可能反转这个选择吗?
var item = $('#someitemparent').html($('#someitem'));
Run Code Online (Sandbox Code Playgroud)
是否可以将其反转为其他所有内容但不是#someitem?
<div id='#someitemparent'>
<div id='#someitem'>
....
</div>
... otheritems
</div>
Run Code Online (Sandbox Code Playgroud) 为什么会这样
var drop = document.getElementById('sidebar-left');
if(drop.addEventListener) {
drop.addEventListener('dragenter', handleDragover, false);
drop.addEventListener('dragover', handleDragover, false);
drop.addEventListener('drop', dropIn, false);
}
Run Code Online (Sandbox Code Playgroud)
有效,但是
var drop = $('sidebar-left')[0];
if(drop.addEventListener) {
drop.addEventListener('dragenter', handleDragover, false);
drop.addEventListener('dragover', handleDragover, false);
drop.addEventListener('drop', dropIn, false);
}
Run Code Online (Sandbox Code Playgroud)
不起作用?据我所知,两者都应该有效.
我知道有人问过这个问题; 而且我已经阅读了大部分被问到的问题都无济于事.我还是遇到了麻烦.我想单击图像并更改图像源(因此图像更改).
这是我的HTML:
<img id="picture1" onclick="showHide('builder1'); change('picture1');" width="25" height="25" src="pictures/down.png">
Run Code Online (Sandbox Code Playgroud)
忽略"showHide('builder1');".它正在发挥作用.我需要帮助改变('picture1').
这是我的Javascript:
function change(id)
{
var isDown = true;
var picture = document.getElementById(id);
if(isDown === true)
{
picture.src = "pictures/pullup.png";
isDown = false; //No longer a down arrow...
}
else
{
picture.src = "pictures/down.png";
isDown = true;
}
}
Run Code Online (Sandbox Code Playgroud)
我能够将图片更改一次(到'pictures/pullup.png'),但无法将其更改回来.我还在if/else语句的某些点添加了警报,以查看它在哪里; 它甚至从未达到声明中的"其他"部分.
javascript ×11
jquery ×3
html ×2
node.js ×2
variables ×2
css ×1
dom ×1
python ×1
setattribute ×1
sql ×1
sql-server ×1