Questions tagged «javascript»

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

3
ECMAScript 2015:for循环中的const
以下两个(或都不/两个)代码片段中的哪个应在完整的ECMAScript 2015实现中工作: for (const e of a) for (const i = 0; i < a.length; i += 1) 据我了解,第一个示例应该有效,因为它e是为每次迭代初始化的。i在第二个版本中也不应该这样吗? 我很困惑,因为现有的实现(Babel,IE,Firefox,Chrome,ESLint)似乎不一致,并且具有的完整实现,并且具有const两个循环变体的各种行为。我也无法在标准中找到具体点,因此将不胜感激。

10
如何在JavaScript中将参数传递给匿名函数?
我试图弄清楚如何将参数传递给JavaScript中的匿名函数。 查看此示例代码,我想您会明白我的意思的: <input type="button" value="Click me" id="myButton" /> <script type="text/javascript"> var myButton = document.getElementById("myButton"); var myMessage = "it's working"; myButton.onclick = function(myMessage) { alert(myMessage); }; </script> 单击按钮时,it's working将出现消息:。但是,myMessage匿名函数中的变量为null。 jQuery使用许多匿名函数,传递该参数的最佳方法是什么?

3
如何使用Javascript访问SVG元素
我搞砸了SVG,希望能在Illustrator中创建SVG文件并使用Javascript访问元素。 这是Illustrator推出的SVG文件(它似乎也向我删除的文件的开头添加了一些垃圾) <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="276.843px" height="233.242px" viewBox="0 0 276.843 233.242" enable-background="new 0 0 276.843 233.242" xml:space="preserve"> <path id="delta" fill="#231F20" d="M34.074,86.094L0,185.354l44.444,38.519l80.741-0.74l29.63-25.186l-26.667-37.037 c0,0-34.815-5.926-37.778-6.667s-13.333-28.889-13.333-28.889l7.407-18.519l31.111-2.963l5.926-21.481l-12.593-38.519l-43.704-5.185 L34.074,86.094z"/> <path id="cargo" fill="#DFB800" d="M68.148,32.761l43.704,4.445l14.815,42.963l-7.407,26.667l-33.333,2.963l-4.444,14.074 l54.074-1.481l22.222,36.296l25.926-3.704l25.926-54.074c0,0-19.259-47.408-21.481-47.408s-31.852-0.741-31.852-0.741 l-19.259-39.259L92.593,8.316L68.148,32.761z"/> <polygon id="beta" fill="#35FF1F" points="86.722,128.316 134.593,124.613 158.296,163.872 190.889,155.724 …

12
使用jQuery获取选定复选框的值
我想遍历复选框组“ locationthemes”,并使用所有选定值构建一个字符串。所以,当复选框2和4被选出的结果将是:“3,8” <input type="checkbox" name="locationthemes" id="checkbox-1" value="2" class="custom" /> <label for="checkbox-1">Castle</label> <input type="checkbox" name="locationthemes" id="checkbox-2" value="3" class="custom" /> <label for="checkbox-2">Barn</label> <input type="checkbox" name="locationthemes" id="checkbox-3" value="5" class="custom" /> <label for="checkbox-3">Restaurant</label> <input type="checkbox" name="locationthemes" id="checkbox-4" value="8" class="custom" /> <label for="checkbox-4">Bar</label> 我在这里检查过:http : //api.jquery.com/checked-selector/,但是没有示例如何通过其名称选择复选框组。 我怎样才能做到这一点?

4
RegExp.exec()偶尔返回NULL
我为此感到非常疯狂,并且我已经花费了不成比例的时间来试图弄清这里正在发生什么。所以请帮我=) 我需要对JavaScript中的字符串进行RegExp匹配。不幸的是,它的行为非常奇怪。这段代码: var rx = /(cat|dog)/gi; var w = new Array("I have a cat and a dog too.", "There once was a dog and a cat.", "I have a cat and a dog too.", "There once was a dog and a cat.","I have a cat and a dog too.", "There …
83 javascript  regex 


12
如何检查iframe是否已加载或是否包含内容?
我有一个id =“ myIframe”的iframe,这里是我加载其内容的代码: $('#myIframe').attr("src", "my_url"); 问题是有时加载时间太长,有时加载速度非常快。所以我必须使用“ setTimeout”函数: setTimeout(function(){ if (//something shows iframe is loaded or has content) { //my code } else { $('#myIframe').attr("src",""); //stop loading content } },5000); 我唯一想知道的是如何确定是否已加载iFrame或是否包含内容。使用iframe.contents().find()将不起作用。我不能用iframe.load(function(){})。

2
如何用JavaScript编写扩展方法?
我需要在JS中编写一些扩展方法。我知道如何在C#中做到这一点。例: public static string SayHi(this Object name) { return "Hi " + name + "!"; } 然后由以下人员调用: string firstName = "Bob"; string hi = firstName.SayHi(); 我将如何在JavaScript中执行类似的操作?

8
使用Javascript加载jQuery并使用jQuery
我使用以下方法将jQuery库附加到dom: var script = document.createElement('script'); script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'; script.type = 'text/javascript'; document.getElementsByTagName('head')[0].appendChild(script); 但是,当我运行时: jQuery(document).ready(function(){... 控制台报告错误: Uncaught ReferenceError: jQuery is not defined 如何动态加载jQuery,以及如何将其放入dom中使用它?

8
如何在JavaScript中实现锁定
如何lock在JavaScript中实现与C#等效的功能? 因此,解释一下我在想一个简单的用例是: 用户单击按钮B。 B引发一个onclick事件。如果B是在event-state事件等待B是在ready-state传播之前。如果Binready-state中的B被锁定并设置为event-state,则事件传播。事件的传播完成后,B设置为ready-state。 我可以看到仅通过ready-state在按钮中添加和删​​除类,就能完成与此类似的事情。但是,问题在于用户连续单击按钮的时间比设置变量的速度快两次,因此在某些情况下,这种锁定尝试将失败。 有谁知道如何实现在JavaScript中不会失败的锁?

5
如何在React Redux中访问存储状态?
我只是做一个简单的应用程序来学习与redux异步。我已使所有工作正常进行,现在我只想在网页上显示实际状态。现在,我实际上如何在render方法中访问商店的状态? 这是我的代码(所有内容都在一页中,因为我只是在学习): const initialState = { fetching: false, fetched: false, items: [], error: null } const reducer = (state=initialState, action) => { switch (action.type) { case "REQUEST_PENDING": { return {...state, fetching: true}; } case "REQUEST_FULFILLED": { return { ...state, fetching: false, fetched: true, items: action.payload } } case "REQUEST_REJECTED": { …



6
关闭(不离开)页面时的jQuery beforeunload?
如何显示“您确定要离开页面吗?” 当用户实际上试图关闭页面(单击浏览器窗口或选项卡上的X按钮)时,而不是当用户试图离开页面导航(单击另一个链接)时。 我的客户希望用户尝试关闭页面时显示一条消息“您确定要离开页面吗?您的购物车中仍然有商品。” 不幸的是$(window).bind('beforeunload'),仅当用户关闭页面时才会触发。 jQuery的: function checkCart() { $.ajax({ url : 'index.php?route=module/cart/check', type : 'POST', dataType : 'json', success : function (result) { if (result) { $(window).bind('beforeunload', function(){ return 'leave?'; }); } } }) }


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.