how to check if a string contains substring and color it in javascript?

I have to create: 1 <input type="text"> 1 <textarea> 1 <div> 1 <button> I have to fill the div with the textarea 's content but if the content contains the input 's string, I have to color it with <span> . For example: If the input contains "is" and the textarea contains "this is a beautiful day", I should put the f

如何检查一个字符串是否包含子字符串并在javascript中对其进行着色?

我必须创建: 1 <input type="text"> 1 <textarea> 1 <div> 1 <button> 我必须用textarea的内容填充div ,但如果内容包含input的字符串,则必须使用<span>为它着色。 例如: 如果input包含“is”,并且textarea包含“这是美好的一天”,我应该在div显示以下文本“这是美好的一天”,并在每次显示“is”字符串时都显示颜色。 我应该使用indexOf()和一个循环。 我有这个: var

jQuery UI dropzone wrong offset inside scrolled iframe

I'm having an issue with jQuery-UI draggables and droppables. I need to drag an draggable inside an droppable which is placed inside an iframe. This works ok until I scroll the iframe. The droppable coordinates are not updated. The issue is demonstrated in this fiddle I'm using the workaround below to make drag and dropping to iframes possible in the first place. It calculates the

滚动iframe中的jQuery UI dropzone错误偏移量

我遇到了jQuery-UI可拖动和拖放的问题。 我需要拖放放置在iframe中的可拖放内容。 这工作正常,直到我滚动iframe。 可放置的坐标不会更新。 这个问题在这个小提琴中得到了证明 我正在使用下面的解决方法,首先将拖放到iframe中。 它会计算正确的偏移量,但不会使用iframe的滚动偏移量。 我尝试过,但无法调整,因此需要考虑滚动偏移量。 // Create new object to cache iframe offsets $.ui.ddmanager.frameOffsets = {

Comparing Arrays of Objects in JavaScript

I want to compare 2 arrays of objects in JavaScript code. The objects have 8 total properties, but each object will not have a value for each, and the arrays are never going to be any larger than 8 items each, so maybe the brute force method of traversing each and then looking at the values of the 8 properties is the easiest way to do what I want to do, but before implementing, I wanted to see i

在JavaScript中比较对象的数组

我想在JavaScript代码中比较2个对象数组。 这些对象有8个总属性,但每个对象不会有每个对象的值,并且每个对象的数组永远不会超过8个项目,所以可能使用遍历每个对象的强力方法,然后查看8个属性是做我想做的最简单的方法,但在实施之前,我想看看是否有人有更优雅的解决方案。 有什么想法吗? 编辑:你不能在当前常用的基于浏览器的JavaScript解释器实现中重载运算符。 要回答原始问题,有一种方法可以做到这一点,并且介

Checking if element exists in array without iterating through it

my array: tempListArray = "[{"id":"12","value":false},{"id":"10","value":false},{"id":"9","value":false},{"id":"8","value":false}]"; To check if an element exists I would do this: for (var i in tempListArray) { //check flag if (tempListArray[i].id == Id) { flagExistsLoop = 1; break; } } Is there anyway, I can check if an Id exists without looping through the whole

检查数组中是否存在元素而不迭代它

我的阵列: tempListArray = "[{"id":"12","value":false},{"id":"10","value":false},{"id":"9","value":false},{"id":"8","value":false}]"; 要检查一个元素是否存在,我会这样做: for (var i in tempListArray) { //check flag if (tempListArray[i].id == Id) { flagExistsLoop = 1; break; } } 无论如何,我可以检查Id是否存在,而不会循环遍历整个数组。 如果说我有100个元素,基本上

Determine if string is in list in JavaScript

In SQL we can see if a string is in a list like so: Column IN ('a', 'b', 'c') What's a good way to do this in JavaScript? It's so clunky to do this: if (expression1 || expression2 || str === 'a' || str === 'b' || str === 'c') { // do something } And I'm not sure about the performance or clarity of this: if (expression1 || expression2 || {a:1, b:1, c:1}[str]) { // do someth

确定字符串是否在JavaScript中列表中

在SQL中,我们可以看到一个字符串是否在列表中,如下所示: Column IN ('a', 'b', 'c') 在JavaScript中做这件事的好方法是什么? 这样做太笨拙了: if (expression1 || expression2 || str === 'a' || str === 'b' || str === 'c') { // do something } 我不确定这个的性能或清晰度: if (expression1 || expression2 || {a:1, b:1, c:1}[str]) { // do something } 或者可以使用开关功能: var str = 'a', flag

MEANJS boilerplate : where to include custom javascript files?

I started using the Mean JS boilerplate (ref website) and would like to know where the recommended place to include public custom javascript, jQuery files (ex. FacebookSDK, jQuery animations,...) . I'm assuming it's going to be somewhere in the public folder. The default structure is as follows : Should it go in modules or lib folder? Can you give more guidelines on what the functio

MEANJS样板文件:其中包含自定义JavaScript文件?

我开始使用Mean JS样板(参考网站),并想知道推荐位置包括公共自定义JavaScript,jQuery文件(例如FacebookSDK,jQuery动画,...)。 我假设它将在公共文件夹中的某处。 默认结构如下: 它应该放在模块还是lib文件夹中? 你能给出更多关于每个文件夹的功能的指导吗? 任何指引? 这是一个关于Angular app文件夹结构的好文章:https://scotch.io/tutorials/angularjs-best-practices-directory-structure 要回答你关

Using function.prototype.bind directly on function declaration

Why is this allowed ? var f = function() { console.log(this.x); }.bind({x:1})(); And why this is not or better why I get syntax error in this case ? function f() { console.log(this.x); }.bind({x:1})(); So, why I need function expression syntax to get this work and is there a way to use bind method directly on function declaration ? The second example works but the syntax is slightly off

直接在函数声明中使用function.prototype.bind

为什么这是允许的? var f = function() { console.log(this.x); }.bind({x:1})(); 为什么这不是或更好,为什么我在这种情况下得到语法错误? function f() { console.log(this.x); }.bind({x:1})(); 那么,为什么我需要函数表达式语法来获得这个工作,并且有没有一种方法可以直接在函数声明中使用bind方法? 第二个例子有效,但语法稍微偏离了一点: 围绕着功能在parens。 我不得不说,我不完全确定为什么。 它似

key binding in react.js

was trying to implement key binding in react.js. did some research, but still wondering what's the cleanest way to do it. For example, if (event.keyCode == 13 /*enter*/) { function() } if (event.keyCode == 27 /*esc*/) { anotherfunction() } I ended up binding the keydown event when the component mounted and unmounted: ... componentDidMount: function() { $(document.body).on('keydow

键在react.js中绑定

试图在react.js中实现键绑定。 做了一些研究,但仍然想知道最干净的方法是什么。 例如, if (event.keyCode == 13 /*enter*/) { function() } if (event.keyCode == 27 /*esc*/) { anotherfunction() } 当组件挂载和卸载时,我最终绑定了keydown事件: ... componentDidMount: function() { $(document.body).on('keydown', this.handleKeyDown); }, componentWillUnMount: function() { $(document.body).off('k

Netbeans additional unneeded code hints

When i'm in Netbeans 8.0, developing JavaScript and i'm on a line of code like this: var sum = (example / anotherExample) And i type a dot after that so it becomes this: var sum = (example / anotherExample). And press CTRL+Space i get a lists of some code hints with the description of: JS Platform Like this: Whenever I press CTRL+Space for the second time while im on that interf

Netbeans额外的不必要的代码提示

当我在Netbeans 8.0中开发JavaScript时,我在这样一行代码中: var sum = (example / anotherExample) 然后我输入一个点,所以它变成这样: var sum = (example / anotherExample). 然后按CTRL +空格我得到一些代码提示的列表与描述: JS平台 喜欢这个: 每当我在该界面上第二次按CTRL +空格键时,它就会为我提供该行代码所有可能的代码提示列表。 为什么是这样? 这个CTRL+Space是获得有关所有字段,方法等的提示

Why does a results vary based on curly brace placement?

I have read this article, where an example is shown. Please explain why the code snippets below return different results due to changes in the placement of curly the braces. Example with an opening curly brace { on new line. function test() { return { /* <----curly brace in new line */ javascript: "fantastic" }; } var r = test(); try { alert(r.javascript); // does this work...

为什么结果因大括号放置而异?

我已阅读这篇文章,其中显示了一个示例。 请解释下面的代码片断为什么会因大括号的位置变化而返回不同的结果。 带有开口大括号的示例{换行符。 function test() { return { /* <----curly brace in new line */ javascript: "fantastic" }; } var r = test(); try { alert(r.javascript); // does this work...? } catch (e) { alert('no - it broke: ' + typeof r); } test()返回undefined 。 带开启