Questions tagged «javascript»

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


9
ReactJS:setTimeout()不起作用?
请记住以下代码: var Component = React.createClass({ getInitialState: function () { return {position: 0}; }, componentDidMount: function () { setTimeout(this.setState({position: 1}), 3000); }, render: function () { return ( <div className="component"> {this.state.position} </div> ); } }); ReactDOM.render( <Component />, document.getElementById('main') ); 难道不应该在3秒后改变状态吗?它立即改变。 我的主要目标是每3秒更改一次状态(使用setInterval()),但是由于它不起作用,因此我尝试了setTimeout(),该方法也不起作用。上面有灯吗?谢谢!

5
如何在jQuery中检查输入文件是否为空
JS新手。 我正在尝试使用jQuery / JavaScript提交表单时检查文件输入元素是否为空。我经历了很多解决方案,但没有任何工作对我有用。我正在努力避免/c/fakepath(除非没有其他选择) <input type="file" name="videoFile" id="videoUploadFile" /> 这不起作用: var vidFile = $("#videoUploadFile").value; 获取文件名的唯一方法是使用以下命令: var vidFile = document.getElementById("videoUploadFile").files[0].name; 如果没有可用的文件,则代码将引发错误: 无法读取未定义的属性名称 这是有道理的,因为未设置数组。但是我不知道该怎么做任何错误处理。 我如何正确地抓住文件输入元素videoUploadFile,检查它是否为空,如果它为空,则抛出错误消息?
100 javascript  jquery  html 

10
将变量的值复制到另一个
我有一个变量,其值为JSON对象。我直接将此变量分配给其他变量,以便它们共享相同的值。它是这样工作的: var a = $('#some_hidden_var').val(), b = a; 这有效,并且两者具有相同的值。我使用mousemove事件处理程序来更新b我的应用程序。单击按钮后,我想恢复b为原始值,这意味着该值存储在中a。 $('#revert').on('click', function(e){ b = a; }); 此后,如果我使用相同的mousemove事件处理程序,它将同时更新a和b,而在更早的时候它仅b按预期进行更新。 我为这个问题感到困惑!怎么了
100 javascript  jquery 

7
Razor MVC使用模型数组填充Javascript数组
我正在尝试使用模型中的数组加载JavaScript数组。在我看来,这应该是可能的。 以下两种方法均无效。 无法创建JavaScript循环并使用JavaScript变量通过模型数组进行增量 for(var j=0; j<255; j++) { jsArray = (@(Model.data[j]))); } 无法创建Razor循环,JavaScript超出范围 @foreach(var d in Model.data) { jsArray = d; } 我可以使用它 var jsdata = @Html.Raw(Json.Encode(Model.data)); 但是我不知道为什么我必须使用JSON。 另外,此刻我将其限制为255个字节。将来可能会达到许多MB。

2
Javascript和ESLint中的全局变量
我有多个JavaScript文件,并且在文件中定义了一些全局变量,该全局变量先于其他变量加载。因此,在第一个文件之后加载的所有文件都可以访问全局变量。但是,ESLint将全局变量显示为“未定义”。我不想更改ESLint的规则,我想找到一种优雅的方法来摆脱这些错误消息。有什么线索吗?谢谢


1
尝试在脚本标签上触发onload事件
我正在尝试按顺序加载一组脚本,但是onload事件对我没有触发。 var scripts = [ '//cdnjs.cloudflare.com/ajax/libs/less.js/1.3.3/less.min.js', '//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0-rc.3/handlebars.min.js', MK.host+'/templates/templates.js' ]; function loadScripts(scripts){ var script = scripts.shift(); var el = document.createElement('script'); el.src = script; el.onload = function(script){ console.log(script + ' loaded!'); if (scripts.length) { loadScripts(scripts); } else { console.log('run app'); MK.init(); } }; $body.append(el); } loadScripts(scripts); 我猜想当使用jQuery将元素添加到DOM时,不会触发诸如el.onload之类的本地事件。如果我使用本机,document.body.appendChild(el)则可以正常启动。

6
Angular2:渲染没有包装标签的组件
我正在努力寻找一种方法来做到这一点。在父组件中,模板描述了table及其thead元素,但将渲染委托tbody给了另一个组件,如下所示: <table> <thead> <tr> <th>Name</th> <th>Time</th> </tr> </thead> <tbody *ngFor="let entry of getEntries()"> <my-result [entry]="entry"></my-result> </tbody> </table> 每个myResult组件都呈现自己的tr标签,基本上是这样的: <tr> <td>{{ entry.name }}</td> <td>{{ entry.time }}</td> </tr> 我之所以没有将其直接放在父组件中(避免需要使用myResult组件),是因为myResult组件实际上比这里显示的要复杂,因此我想将其行为放在单独的组件和文件中。 产生的DOM看起来很糟。我相信这是因为它无效,因为tbody只能包含tr元素(请参见MDN),但是我生成的(简化的)DOM是: <table> <thead> <tr> <th>Name</th> <th>Time</th> </tr> </thead> <tbody> <my-result> <tr> <td>Bob</td> <td>128</td> </tr> </my-result> </tbody> <tbody> <my-result> <tr> <td>Lisa</td> <td>333</td> </tr> </my-result> …

9
添加onclick函数以在JavaScript中转到url?
当用户将鼠标悬停在该字段上时,我使用了一些精美的JavaScript来突出显示该字段。您能否告诉我是否有添加onclick功能的方法,该功能将充当链接并转到URL? <script> $(function() { $('tr').hover(function() { $(this).css('background-color', '#eee'); $(this).contents('td').css({'border': '0px solid red', 'border-left': 'none', 'border-right': 'none'}); $(this).contents('td:first').css('border-left', '0px solid red'); $(this).contents('td:last').css('border-right', '0px solid red'); }, function() { $(this).css('background-color', '#FFFFFF'); $(this).contents('td').css('border', 'none'); $('a#read_message.php').click(function(){ URL(); }); }); }); </script>

5
从XmlHttpRequest.responseJSON解析JSON
我正在尝试在JavaScript中解析bit.ly JSON响应。 我通过XmlHttpRequest获取JSON。 var req = new XMLHttpRequest; req.overrideMimeType("application/json"); req.open('GET', BITLY_CREATE_API + encodeURIComponent(url) + BITLY_API_LOGIN, true); var target = this; req.onload = function() {target.parseJSON(req, url)}; req.send(null); parseJSON: function(req, url) { if (req.status == 200) { var jsonResponse = req.responseJSON; var bitlyUrl = jsonResponse.results[url].shortUrl; } 我在Firefox插件中执行此操作。当我运行时,该行出现错误“ jsonResponse is undefined” var bitlyUrl …

4
为什么在渲染时调用我的onClick?-React.js
我有一个已创建的组件: class Create extends Component { constructor(props) { super(props); } render() { var playlistDOM = this.renderPlaylists(this.props.playlists); return ( <div> {playlistDOM} </div> ) } activatePlaylist(playlistId) { debugger; } renderPlaylists(playlists) { return playlists.map(playlist => { return <div key={playlist.playlist_id} onClick={this.activatePlaylist(playlist.playlist_id)}>{playlist.playlist_name}</div> }); } } function mapStateToProps(state) { return { playlists: state.playlists } } export …


6
将ES6可迭代转换为数组
假设您有一个类似数组的Javascript ES6 Iterable,您事先知道它的长度是有限的,那么将其转换为Javascript数组的最佳方法是什么? 这样做的原因是,许多下划线和lodash之类的js库仅支持Arrays,因此,如果您希望在Iterable上使用其任何功能,则必须首先将其转换为Array。 在python中,您可以只使用list()函数。ES6中是否有等效功能?


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.