清除select下拉选项,添加并选择特点选项

$('#mySelect')
.find('option')
.remove()
.end()
.append('<option value="whatever">text</option>')
.val('whatever')
;

ref:http://stackoverflow.com/questions/47824/how-do-you-remove-all-the-options-of-a-select-box-and-then-add-one-option-and-se

为字符串添加endsWith的方法

String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

ref :http://stackoverflow.com/questions/280634/endswith-in-javascript

同理,字符串startsWith方法

if (typeof String.prototype.startsWith != 'function') {
// see below for better implementation!
String.prototype.startsWith = function (str){
return this.indexOf(str) === 0;
};
}

ref : http://stackoverflow.com/questions/646628/how-to-check-if-a-string-startswith-another-string

为数组添加remove方法

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};

ref: http://ejohn.org/blog/javascript-array-remove/#postcomment

判断HTML元素是否有子元素

if ( $('#myfav').children().size() > 0 ) {
// do something
}

or

if ($('#myfav').is(':parent')) {
// do something
}

ref:http://stackoverflow.com/questions/1526873/jquery-if-div-id-has-children

实用Javascript代码片段的更多相关文章

  1. 转--Android实用的代码片段 常用代码总结

    这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下     1:查看是否有存储卡插入 复制代码 代码如下: String status=Environment.getE ...

  2. 精心收集的 48 个 JavaScript 代码片段,仅需 30 秒就可理解!

    原文:https://github.com/Chalarangelo/30-seconds-of-code#anagrams-of-string-with-duplicates 作者:Chalaran ...

  3. 精心收集的 48 个 JavaScript 代码片段,仅需 30 秒就可理解

    原文:Chalarangelo  译文:IT168 https://github.com/Chalarangelo/30-seconds-of-code#anagrams-of-string-with ...

  4. 精心收集的48个JavaScript代码片段,仅需30秒就可理解

    源文链接 :https://github.com/Chalarangelo/30-seconds-of-code#anagrams-of-string-with-duplicates 该项目来自于 G ...

  5. 绝对应当收藏的10个实用HTML5代码片段(转)

    HTML5绝对是一个流行元素,受到如此多的公司组织的追捧,作为极客来说,岂能错过呢?在今天这篇文章中,我们将分享一些超实用的HTML5的代码片段,相信大家一定会喜欢! 正确的嵌入flash 如果你经常 ...

  6. 超实用的 JavaScript 代码片段( ES6+ 编写)

    Array 数组 Array concatenation (数组拼接) 使用 Array.concat() ,通过在 args 中附加任何数组 和/或 值来拼接一个数组. const ArrayCon ...

  7. 实用jQuery代码片段

    maco精选的一些jQuery代码,也许你从中可以举一反三[代码] [JavaScript]代码001<p>002    <h3><span >★ 使用jQuery ...

  8. 精彩 JavaScript 代码片段

    1. 根据给定的条件在原有的数组上,得到所需要的新数组. ——<JavaScript 王者归来> var a = [-1,-1,1,2,-2,-2,-3,-3,3,-3]; functio ...

  9. 为开发者准备的9个实用PHP代码片段

    一.查看邮件是否已被阅读 当你发送邮件时,你肯定很想知道你的邮件是否已被对方查看.下面的代码就能实现记录阅读你邮件的IP地址,还有实际的阅读日期和时间. error_reporting(0);Head ...

随机推荐

  1. 用PHP实现定时器功能

    1.直接使用PHP来完成定时 <?php ignore_user_abort(false);//当用户关闭页面时服务停止 set_time_limit(0); //设置执行时间,单位是秒.0表示 ...

  2. HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)

    Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...

  3. 项目如何脱离TFS 2010的管理

    在VS 里,文件->源代码管理->更改源代码管理->取消绑定.

  4. Java Hour 41 Maven ( 3 )

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 41.1 m2eclipse 简介 m2eclipse 是一款开源工具,为ec ...

  5. hdu 3466 排序01背包

    也是好题,带限制的01背包,先排序,再背包 这题因为涉及到q,所以不能直接就01背包了.因为如果一个物品是5 9,一个物品是5 6,对第一个进行背包的时候只有dp[9],dp[10],…,dp[m], ...

  6. hdu 1166 线段树单点更新

    等线段树复习完再做个总结 1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 10 2Add 6 3Query 3 10End Case 1:633 ...

  7. Java编程语言中sleep()和yield()的区别

    转自:http://developer.51cto.com/art/201003/189465.htm 1. Thread.yield():     api中解释: 暂停当前正在执行的线程对象,并执行 ...

  8. 【练习】使用接口回调和handler实现数据加载到listview

    代码结构 布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...

  9. Linux常用命令_(系统设置)

    基本命令:clear 指令名称:clear指令所在路径:/usr/bin/clear执行权限:All User语法:clear功能描述:清空终端屏幕显示.范例:$ clear 环境变量:alias.e ...

  10. stack UVA 442 Matrix Chain Multiplication

    题目传送门 题意:给出每个矩阵的行列,计算矩阵的表达式,如果错误输出error,否则输出答案 分析:表达式求值,stack 容器的应用:矩阵的表达式求值A 矩阵是a * b,B 矩阵是b * c,则A ...