草庐IT

if-then-else

全部标签

javascript - 在 'If mobile phone' 中

我正在考虑使用jQuery.browser做一些事情,但这只会返回您所在的浏览器以及它是否是webkit等。所以我基本上想关闭某些js文件,如果您在移动设备上,甚至不加载它?我想你可以做到,但是怎么做呢? 最佳答案 我认为thisanswer更好,因为它不依赖于屏幕宽度:if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)){//somecode..}我知道现在您已经有了品牌浏览器依赖项,但这比检查屏幕尺寸更易于维护。

javascript - 是 !!检查 if 语句中真值的最佳实践

在angular.js中,有一些代码片段使用!!来检查一个值在if条件下是否为真。这是最佳做法吗?我完全理解返回值或其他任务!用于确保类型为bool值。但是条件检查也是如此吗?if(!!value){element[name]=true;element.setAttribute(name,lowercasedName);}else{element[name]=false;element.removeAttribute(lowercasedName);} 最佳答案 !!value通常用作将value强制为true或false的方法,分

javascript - react : Can I check if a state exists before rendering it

我是React的新手,我制作了一个显示用户名user的导航栏{this.state.name}但问题是如果用户未登录,我会收到一个错误,因为this.state.name未定义。有什么方法可以在将它呈现为导航栏的一部分之前检查是否已定义this.state.name还是有更好的方法来消除此错误? 最佳答案 当然,使用三元:render(){return(this.state.name?{this.state.name}:null);}甚至更短render(){return(this.state.name&&{this.state.n

javascript - Javascript if 语句中的求值顺序

为了保护我的代码不访问我使用的未声明的变量if(typeofmyVar!='undefined')这很好用,但我想在其中添加另一个if语句。像这样转换:if(typeofmyVar!='undefined'){if(myVar=="test"){}}为此:if(typeofmyVar!='undefined'&&myVar=="test")考虑到myVar可能未定义,这最后的代码在每种使用情况和每个浏览器中是否安全?是否有可能if()中的各种语句未按照它们编写的顺序求值?如果myVar未定义,我可以假设myVar=="test"将永远执行吗? 最佳答案

javascript - 拖放 : How to get the URL of image being dropped if image is a link (not the url of the link)

我有这个代码:functiondrop(evt){evt.stopPropagation();evt.preventDefault();varimageUrl=evt.dataTransfer.getData('URL');alert(imageUrl);}FIDDLE如果你放下元素它会提醒图像的url。到目前为止一切顺利。我的问题是,如果您删除元素它会提醒href的url的元素。我想提醒的网址内的元素就像您在上面的示例中放下图像一样。这可能吗?我不介意使用Jquery或任何其他库。我只想获取中图像的url元素。重点是将其他网站的图片链接拖到我的网站并获取图片的url。为了更清楚地说明

javascript - 为什么 Jasmine 被称为 "BDD"测试框架,即使不支持 "Given/When/Then"?

在介绍Jasmine,它说:Jasmineisabehavior-drivendevelopmentframeworkfortestingJavaScriptcode.我阅读了BDD的几篇文章,似乎我们应该使用“Given/When/Then”来定义“Scenario”,这就是“cucumber”所做的。但是在Jasmine中,我看不到任何这样的方法。即使Jasmine没有这样的概念,我们还能称其为“BDD”测试框架吗? 最佳答案 Jasmine不会阻止您使用given-when-then,下面的示例显示了在使用Jasmine时可以

javascript - 使用带有异步函数和 .then 的 MobX @action 装饰器

我正在使用MobX2.2.2尝试在异步操作中改变状态。我将MobX的useStrict设置为true。@actionsomeAsyncFunction(args){fetch(`http://localhost:8080/some_url`,{method:'POST',body:{args}}).then(res=>res.json()).then(json=>this.someStateProperty=json).catch(error=>{thrownewError(error)});}我得到:Error:Error:[mobx]Invariantfailed:Itisnota

javascript - JavaScript/jQuery 中的 "if mouseover"或 "do while mouseover"

是否有JavaScript或jQuery解决方案可以在鼠标悬停在DOM对象上时重复运行函数(在setTimeout之后)?否则说,是否有JavaScript“鼠标悬停时执行”(或“如果鼠标悬停”)?$('someObject').bind('mouseover',function(){//Dothefollowingwhilemouseover$('someOtherObject').css('margin-left',adjustedLeft+'px');setTimeout(/*doitagain*/,25);}); 最佳答案

javascript - 我可以在内联 javascript if 语句中省略 else 吗?

我正在尝试使用它,但它似乎不起作用。我猜这不是一个选择,但想确认一下。这有效吗?(if_it_is)?thats_cool(); 最佳答案 你可以在那里使用&&:if_it_is&&thats_cool();基本上等于:if(your_expression){thats_cool();} 关于javascript-我可以在内联javascriptif语句中省略else吗?,我们在StackOverflow上找到一个类似的问题: https://stackove

javascript - 类型错误 : Cannot call method 'then' of undefined Angularjs

我是Angular的新手,在进行同步操作时遇到了问题。我已经解决了AngularController出现的几个问题,我从newController文件中得到错误“无法调用未定义的方法”。angular.module('newApp.newController',['angularSpinner','ui.bootstrap']).controller('newController',function($q,$scope,utilityFactory,$http){utilityFactory.getData().then(function(data){console.log("succ