Questions tagged «javascript»

有关在ECMAScript(JavaScript / JS)及其各种方言/实现(不包括ActionScript)中进行编程的问题。此标记很少单独使用,但最常与标记[node.js],[jquery],[json]和[html]关联。

5
如何在React中检测Esc键按下以及如何处理
如何在reactjs上检测Esc按键?类似于jquery $(document).keyup(function(e) { if (e.keyCode == 27) { // escape key maps to keycode `27` // <DO YOUR WORK HERE> } }); 一旦检测到,我想将信息传递给组件。我有3个组件,其中最后一个活动组件需要对逃逸键的反应做出反应。 我在想一种组件激活时的注册方式 class Layout extends React.Component { onActive(escFunction){ this.escFunction = escFunction; } onEscPress(){ if(_.isFunction(this.escFunction)){ this.escFunction() } } render(){ return ( <div class="root"> <ActionPanel onActive={this.onActive.bind(this)}/> <DataPanel onActive={this.onActive.bind(this)}/> <ResultPanel onActive={this.onActive.bind(this)}/> …


9
使用JavaScript获取选定的选项文本
我有一个像这样的下拉列表: <select id="box1"> <option value="98">dog</option> <option value="7122">cat</option> <option value="142">bird</option> </select> 如何使用JavaScript获取实际的选项文本而不是值?我可以通过以下方式获得价值: <select id="box1" onChange="myNewFunction(this.selectedIndex);" > 但不是7122我想要的cat。


16
如何在NodeJs应用程序和模块之间正确重用与Mongodb的连接
我一直在阅读,仍然对在整个NodeJs应用程序中共享同一数据库(MongoDb)连接的最佳方法感到困惑。据我了解,应在应用启动时打开连接,并在模块之间重用。我目前的最佳方法想法是server.js(一切开始的主文件)连接到数据库并创建传递给模块的对象变量。连接后,模块代码将根据需要使用此变量,并且此连接保持打开状态。例如: var MongoClient = require('mongodb').MongoClient; var mongo = {}; // this is passed to modules and code MongoClient.connect("mongodb://localhost:27017/marankings", function(err, db) { if (!err) { console.log("We are connected"); // these tables will be passed to modules as part of mongo object mongo.dbUsers = db.collection("users"); mongo.dbDisciplines = db.collection("disciplines"); console.log("aaa " + users.getAll()); …

7
使用lodash比较数组(项目存在而无顺序)
我知道我可以使用循环来做到这一点,但是我试图找到一种优雅的方式来做到这一点: 我有两个数组: var array1 = [['a', 'b'], ['b', 'c']]; var array2 = [['b', 'c'], ['a', 'b']]; 我想用来lodash确认以上两个数组是相同的。通过“相同的”我的意思是,有没有在项目array1中没有包含在array2。 在检查这些项目之间的相等性方面: ['a', 'b'] == ['b', 'a'] 要么 ['a', 'b'] == ['a', 'b'] 两者都可以工作,因为字母始终是有序的。


14
控制器不是函数,未定义,但在全局定义控制器时
我正在使用angularjs编写示例应用程序。我在chrome浏览器上遇到以下错误。 错误是 错误:[ng:areq] http://errors.angularjs.org/1.3.0-beta.17/ng/areq?p0=ContactController&p1=not%20a%20function%2C%20got%20undefined 呈现为 参数“ ContactController”不是函数,未定义 码 <!DOCTYPE html> <html ng-app> <head> <script src="../angular.min.js"></script> <script type="text/javascript"> function ContactController($scope) { $scope.contacts = ["abcd@gmail.com", "abcd@yahoo.co.in"]; $scope.add = function() { $scope.contacts.push($scope.newcontact); $scope.newcontact = ""; }; } </script> </head> <body> <h1> modules sample </h1> <div ng-controller="ContactController"> Email:<input type="text" ng-model="newcontact"> <button ng-click="add()">Add</button> <h2> Contacts …

2
在标准模式下设置元素的宽度或高度
是否可以<div>在标准模式下的JavaScript中设置HTML元素(例如)的宽度或高度? 请注意以下代码: <html> <script language="javascript" type="text/javascript"> function changeWidth(){ var e1 = document.getElementById("e1"); e1.style.width = 400; } </script> <body> <input type="button" value="change width" onclick="changeWidth()"/> <div id="e1" style="width:20px;height:20px; background-color:#096"></div> </body> </html> 当用户按下更改宽度按钮时,<div>的宽度应更改。 当doctype声明确定Quirks模式时,它可以正常工作。在标准模式下,我无法通过这种方式更改元素的大小 在标准模式下可以操纵元素的大小吗?如何绕过这种功能失调?

4
JavaScript继承:Object.create与New
在JavaScript中,这两个示例之间有什么区别: 先决条件: function SomeBaseClass(){ } SomeBaseClass.prototype = { doThis : function(){ }, doThat : function(){ } } 使用Object.create的继承示例A: function MyClass(){ } MyClass.prototype = Object.create(SomeBaseClass.prototype); 使用new关键字的继承示例B function MyClass(){ } MyClass.prototype = new SomeBaseClass(); 这两个例子似乎做同样的事情。您何时会选择一个? 另一个问题:考虑下面链接(第15行)中的代码,其中对函数自己的构造函数的引用存储在原型中。为什么这有用? https://github.com/mrdoob/three.js/blob/master/src/loaders/ImageLoader.js 摘录(如果您不想打开链接): THREE.ImageLoader.prototype = { constructor: THREE.ImageLoader }

4
在IE9中将任何内容从Javascript传递到VBScript
我有一个用VBScript编写的框架。在此框架的某个函数内部检查函数的If语句中是否为Nothing,然后执行某些操作。使用Java编写的框架的代码。因此,我需要通过Nothing来执行某些操作。在IE8和更早版本中,采用了以下方法: <script type="text/vbscript"> Function Test(val) If (IsNull(val)) Then Test = "Null" ElseIf (IsObject(val)) Then If (val Is Nothing) Then Test = "Nothing" End If End If End Function Dim jsNothing Set jsNothing = Nothing msgBox(Test(jsNothing)) msgBox(Test(Null)) </script> <script type="text/javascript"> alert(Test(jsNothing)); </script> 在IE <9中,输出将为:Nothing,Null和Nothing。 在IE9中:无,无,无。 如何在IE9中将Javathing Nothing传递给VBScript? 抱歉,我知道它很丑,但是我被困了。并讨厌VBScript。 编辑: 有一个框架功能的例子。我无法更改它,因为它已在应用程序中广泛使用。 Function …

13
在AngularJS中格式化日期时间
如何在AngularJS中正确显示日期和时间? 下面的输出同时显示输入和输出,带有和不带有AngularJS日期过滤器: In: {{v.Dt}} AngularJS: {{v.Dt | date:'yyyy-MM-dd HH:mm:ss Z'}} 打印: In: 2012-10-16T17:57:28.556094Z AngularJS: 2012-10-16T17:57:28.556094Z 所需的显示格式为 2010-10-28 23:40:23 0400或2010-10-28 23:40:23 EST



6
jQuery中width,innerWidth和externalWidth,height,innerHeight和externalHeight之间的区别是什么
我写了一些例子来看看有什么区别,但是它们显示出相同的宽度和高度结果。 <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ var div = $('.test'); var width = div.width(); // 200 px var innerWidth = div.innerWidth(); // 200px var outerWidth = div.outerWidth(); // 200px var height = div.height(); // 150 px var innerHeight = div.innerHeight(); // 150 px var outerHeight = div.outerHeight(); …
123 javascript  jquery  css  dom  styles 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.