phpjs

nl2br() equivalent in javascript [duplicate]

匆匆过客 提交于 2019-11-28 06:56:00
问题 Possible Duplicate: jQuery convert line breaks to br (nl2br equivalent) Currently I add <BR> for each evt.which == 13 . Is there a nl2br() for JavaScript, so I can do away with this evt.which == 13 ? How different is this from php.js $('#TextArea').keypress(function(evt) { if (evt.which == 13) { var range = $('#TextArea').getSelection(); var image_selection = range.text; $('#TextArea').replaceSelection('<BR>'); $('#TextArea1').html($('#TextArea').val()); } }); 回答1: Take a look at nl2br on php

Javascript equivalent of PHP's list()

限于喜欢 提交于 2019-11-26 20:27:28
Really like that function. $matches = array('12', 'watt'); list($value, $unit) = $matches; Is there a Javascript equivalent of that? Nicolás There is, but in "new" versions of Javascript: Destructuring assignment - Javascript 1.7 . It's probably only supported in Mozilla-based browsers, and maybe in Rhino. var a = 1; var b = 3; [a, b] = [b, a]; EDIT: actually it wouldn't surprise me if the V8 Javascript library (and thus Chrome) supports this. But don't count on it either :) try this: matches = ['12', 'watt']; [value, unit] = matches; ES6 does support this directly now via array destructuring

JavaScript equivalent of PHP&#39;s in_array()

不问归期 提交于 2019-11-26 11:12:31
Is there a way in JavaScript to compare values from one array and see if it is in another array? Similar to PHP's in_array function? Paolo Bergantino No, it doesn't have one. For this reason most popular libraries come with one in their utility packages. Check out jQuery's inArray and Prototype's Array.indexOf for examples. jQuery's implementation of it is as simple as you might expect: function inArray(needle, haystack) { var length = haystack.length; for(var i = 0; i < length; i++) { if(haystack[i] == needle) return true; } return false; } If you are dealing with a sane amount of array

JavaScript equivalent of PHP&#39;s in_array()

落爺英雄遲暮 提交于 2019-11-26 03:29:50
问题 Is there a way in JavaScript to compare values from one array and see if it is in another array? Similar to PHP\'s in_array function? 回答1: No, it doesn't have one. For this reason most popular libraries come with one in their utility packages. Check out jQuery's inArray and Prototype's Array.indexOf for examples. jQuery's implementation of it is as simple as you might expect: function inArray(needle, haystack) { var length = haystack.length; for(var i = 0; i < length; i++) { if(haystack[i] ==