Questions tagged «javascript»

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

11
JavaScript-获取HTML表单值
如何获取HTML表单的值以传递给JavaScript? 这样对吗?我的脚本有两个参数,一个来自文本框,一个来自下拉框。 <body> <form name="valform" action="" method="POST"> Credit Card Validation: <input type="text" id="cctextboxid" name="cctextbox"><br/> Card Type: <select name="cardtype" id="cardtypeid"> <option value="visa">Visa</option> <option value="mastercard">MasterCard</option> <option value="discover">Discover</option> <option value="amex">Amex</option> <option value="diners">Diners Club</option> </select><br/> <input type="button" name="submit" value="Verify Credit Card" onclick="isValidCreditCard(document.getElementById('cctextboxid').value,document.getElementById('cardtypeid').value)" /> </body>
112 javascript  html 

30
JavaScript中多个数组的笛卡尔积
Наэтотвопросестьответына 堆栈溢出нарусском:Декартовопроизведениенесколькихмассивов 您将如何在JavaScript中实现多个数组的笛卡尔积? 举个例子, cartesian([1, 2], [10, 20], [100, 200, 300]) 应该回来 [ [1, 10, 100], [1, 10, 200], [1, 10, 300], [2, 10, 100], [2, 10, 200] ... ]


5
如何在基于类的组件中使用React.forwardRef?
我正在尝试使用React.forwardRef,但是在如何使它在基于类的组件(而不是HOC)中工作的过程中遇到了麻烦。 该文档示例使用元素和功能组件,甚至将类包装在功能较高的组件中。 因此,从像这样在他们的ref.js文件: const TextInput = React.forwardRef( (props, ref) => (<input type="text" placeholder="Hello World" ref={ref} />) ); 而是将其定义如下: class TextInput extends React.Component { render() { let { props, ref } = React.forwardRef((props, ref) => ({ props, ref })); return <input type="text" placeholder="Hello World" ref={ref} />; } } 要么 class TextInput …


13
使用jQuery检测Safari
尽管两者都是基于Webkit的浏览器,但Safari会在URL中用引号将引号引起来,而Chrome则不会。 因此,我需要在JS中区分这两者。 jQuery的浏览器检测文档将“ safari”标记为已弃用。 有没有更好的方法,或者我现在只是坚持不赞成使用的值?

17
从AngularJS中的对象数组通过ID获取特定对象
我有一个JSON文件,其中包含一些我想在AngularJS网站上访问的数据。现在,我要从数组中仅获取一个对象。因此,我想例如ID为1的商品。 数据如下所示: { "results": [ { "id": 1, "name": "Test" }, { "id": 2, "name": "Beispiel" }, { "id": 3, "name": "Sample" } ] } 我想使用AngularJS $ http功能加载数据,如下所示: $http.get("data/SampleData.json"); 这正在工作。但是,现在如何从获取的数组中获取特定的数据对象(按ID)$http.get呢? 在此先感谢您的帮助。 马克·马克

4
内容可编辑,在文本末尾设置插入符号(跨浏览器)
在Chrome中输出: <div id="content" contenteditable="true" style="border:1px solid #000;width:500px;height:40px;"> hey <div>what's up?</div> <div> <button id="insert_caret"></button> 我相信在FF中它将看起来像这样: hey <br /> what's up? 并在IE中: hey <p>what's up?</p> 不幸的是,没有一种很好的方法来使每个浏览器都插入一个<br />而不是div标签或p标签,或者至少我在网上找不到任何东西。 无论如何,我现在想做的是,当我按下按钮时,我希望将插入符号设置在文本的末尾,因此它应该看起来像这样: hey what's up?| 有什么办法可以使其在所有浏览器中正常工作? 例: $(document).ready(function() { $('#insert_caret').click(function() { var ele = $('#content'); var length = ele.html().length; ele.focus(); //set caret -> end pos } …


14
如何检测在线/离线事件跨浏览器?
我正在尝试使用HTML5联机和脱机事件来准确检测浏览器何时脱机。 这是我的代码: <script> // FIREFOX $(window).bind("online", applicationBackOnline); $(window).bind("offline", applicationOffline); //IE window.onload = function() { document.body.ononline = IeConnectionEvent; document.body.onoffline = IeConnectionEvent; } </script> 当我在Firefox或IE上单击“脱机工作”时,它工作正常,但是当我拔掉电线时,它是随机工作的。 检测此更改的最佳方法是什么?我想避免重复超时的ajax调用。

23
AngularJS浏览器通过使用指令自动填充的解决方法
在AngularJS中提交表单并使用浏览器时,请记住密码功能,并且在随后的登录尝试中,让浏览器使用用户名和密码填写登录表单,该$scope模型不会基于自动填充进行更改。 我发现的唯一肮脏的技巧是使用以下指令: app.directive("xsInputSync", ["$timeout" , function($timeout) { return { restrict : "A", require: "?ngModel", link : function(scope, element, attrs, ngModel) { $timeout(function() { if (ngModel.$viewValue && ngModel.$viewValue !== element.val()) { scope.apply(function() { ngModel.$setViewValue(element.val()); }); } console.log(scope); console.log(ngModel.$name); console.log(scope[ngModel.$name]); }, 3000); } }; }]); 问题在于,ngModel.$setViewValue(element.val());基于element.val()返回的值不会更改模型或视图。我该怎么做?


7
使用Webpack将jQuery公开到真实的Window对象
我想将jQuery对象公开给可在浏览器的开发人员控制台内部访问的全局窗口对象。现在在我的webpack配置中,我有以下几行: plugins: [ new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }) ] 这些行将jQuery定义添加到我的webpack模块中的每个文件中。但是,当我构建项目并尝试像这样在开发人员控制台中访问jQuery时: window.$; window.jQuery; 它说这些属性是不确定的... 有没有办法解决这个问题?


15
如何判断<script>标签是否加载失败
我正在向&lt;script&gt;页面的中动态添加标签&lt;head&gt;,并且我想知道加载是否以某种方式失败-404,加载的脚本中的脚本错误等。 在Firefox中,这有效: var script_tag = document.createElement('script'); script_tag.setAttribute('type', 'text/javascript'); script_tag.setAttribute('src', 'http://fail.org/nonexistant.js'); script_tag.onerror = function() { alert("Loading failed!"); } document.getElementsByTagName('head')[0].appendChild(script_tag); 但是,这在IE或Safari中不起作用。 有谁知道在Firefox以外的浏览器中实现此目的的方法吗? (我认为不需要在.js文件中放置特殊代码的解决方案是一个很好的解决方案。这种解决方案既笨拙又不灵活。)

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.