清除select option selected属性失效

海上小岛嗯 2018-06-15 10:04:31
html
<select class="form-control" id="taskType">
<option value="1">抓捕行动</option>
<option value="2" selected="selected">侦查行动</option>
</select>

js
$("#taskType").find("option[value='1']").removeAttr('selected');

显示页面


这是什么情况啊?bug吗?
...全文
773 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ambit_tsai-微信 2018-06-15
  • 打赏
  • 举报
回复
引用 6 楼 海上小岛嗯的回复:
所以我做了,清除(选中1) removeAttr('selected') 但是没有作用,我进入页面调试,发现1的选中状态已被清除掉,但是页面展示还是选中1的状态
下拉框默认选择第一个
海上小岛嗯 2018-06-15
  • 打赏
  • 举报
回复
所以我做了,清除(选中1) removeAttr('selected') 但是没有作用,我进入页面调试,发现1的选中状态已被清除掉,但是页面展示还是选中1的状态
海上小岛嗯 2018-06-15
  • 打赏
  • 举报
回复
我的叙述少了一点 在页面之后赋值了数据变成了 1选中 , 重新打开弹窗页面时,清空之前的操作,让select 重新选中2
ambit_tsai-微信 2018-06-15
  • 打赏
  • 举报
回复
如果你是要清空下拉框的值
$("#taskType").val('');
ambit_tsai-微信 2018-06-15
  • 打赏
  • 举报
回复
你写错了啊,value='2',不是1

$("#taskType").find("option[value='2']").removeAttr('selected');
qq_34408221 2018-06-15
  • 打赏
  • 举报
回复
不明白你要表达的意思 你的意思是删除1 的属性就显示2? 这样明显是错误的
Hello World, 2018-06-15
  • 打赏
  • 举报
回复
它总得选中一个吧?1不选,2也没选,1在前面,默认还是选中了
天际的海浪 2018-06-15
  • 打赏
  • 举报
回复
$("#taskType").prop("selectedIndex",1);
海上小岛嗯 2018-06-15
  • 打赏
  • 举报
回复
引用 8 楼 apollokk 的回复:
你要选中值为2的选项的话用$("#taskType").val('2');就可以了,不用removeAttr……
已解决
Hello World, 2018-06-15
  • 打赏
  • 举报
回复
你要选中值为2的选项的话用$("#taskType").val('2');就可以了,不用removeAttr……
jQuery1.2 API 中文版折叠展开折叠全部展开全部 英文说明 核心jQuery 核心函数 jQuery(expression,[context]) jQuery(expression,[context]) 这个函数接收一个包含 CSS 选择器的字符串,然后用这个字符串去匹配一组元素。 jQuery 的核心功能都是通过这个函数实现的。 jQuery中的一切都构建于这个函数之上,或者说都是在以某种方式使用这个函数。这个函数最基本的用法就是向它传递一个表达式(通常由 CSS 选择器组成),然后根据这个表达式来查找所有匹配的元素。 默认情况下, 如果没有指定context参数,$()将在当前的 HTML 文档中查找 DOM 元素;如果指定了 context 参数,如一个 DOM 元素集或 jQuery 对象,那就会在这个 context 中查找。 参考 Selectors 获取更多用于 expression 参数的 CSS 语法的信息。 -------------------------------------------------------------------------------- This function accepts a string containing a CSS selector which is then used to match a set of elements. The core functionality of jQuery centers around this function. Everything in jQuery is based upon this, or uses this in some way. The most basic use of this function is to pass in an expression (usually consisting of CSS), which then finds all matching elements. By default, if no context is specified, $() looks for DOM elements within the context of the current HTML document. If you do specify a context, such as a DOM element or jQuery object, the expression will be matched against the contents of that context. See Selectors for the allowed CSS syntax for expressions. 返回值 jQuery 参数 expression (String) : 用来查找的字符串 context (Element, jQuery) : (可选) 作为待查找的 DOM 元素集、文档或 jQuery 对象。 示例 找到所有 p 元素,并且这些元素都必须是 div 元素的子元素。 HTML 代码:

one

two

three

jQuery 代码: $("div > p"); 结果: [

two

] -------------------------------------------------------------------------------- 在文档的第一个表单中,查找所有的单选按钮(即: type 值为 radio 的 input 元素)。 jQuery 代码: $("input:radio", document.forms[0]); -------------------------------------------------------------------------------- 在一个由 AJAX 返回的 XML 文档中,查找所有的 div 元素。 jQuery 代码: $("div", xml.responseXML); jQuery(html)jQuery(html) 根据提供的原始 HTML 标记字符串,动态创建由 jQuery 对象包装的 DOM 元素。 你可以传递一个手写的 HTML 字符串,或者由某些模板引擎或插件创建的字符串,也可以是通过 AJAX 加载过来的字符串。但是在你创建 input 元素的时会有限制,可以参考第二个示例。当然这个字符串可以包含斜杠 (比如一个图像地址),还有反斜杠。当你创建单个元素时,请使用闭合标签或 XHTML 格式。例如,创建一个 span ,可以用 $("") 或 $("") ,但不推荐 $("") -------------------------------------------------------------------------------- Create DOM elements on-the-fly from the provided String of raw HTML. You can pass in plain HTML Strings written by hand, create them using some template engine or plugin, or load them via AJAX. There are limitations when creating input elements, see the second example. Also when passing strings that may include slashes (such as an image path), escape the slashes. When creating single elements use the closing tag or XHTML format. For example, to create a span use $("") or $("") instead of without the closing slash/tag. 返回值 jQuery 参数 html (String) : 用于动态创建DOM元素的HTML标记字符串 示例 动态创建一个 div 元素(以及其中的所有内容),并将它追加到 body 元素中。在这个函数的内部,是通过临时创建一个元素,并将这个元素的 innerHTML 属性设置为给定的标记字符串,来实现标记到 DOM 元素转换的。所以,这个函数既有灵活性,也有局限性。 jQuery 代码: $("

Hello

").appendTo("body"); -------------------------------------------------------------------------------- 创建一个 元素必须同时设定 type 属性。因为微软规定 元素的 type 只能写一次。 jQuery 代码: // 在 IE 中无效: $("").attr("type", "checkbox"); // 在 IE 中有效: $(""); jQuery(elements)jQuery(elements) 将一个或多个DOM元素转化为jQuery对象。 这个函数也可以接收XML文档和Window对象(虽然它们不是DOM元素)作为有效的参数。 -------------------------------------------------------------------------------- Wrap jQuery functionality around a single or multiple DOM Element(s). This function also accepts XML Documents and Window objects as valid arguments (even though they are not DOM Elements). 返回值 jQuery 参数 elements (Element, Array) : 用于封装成jQuery对象的DOM元素 示例 设置页面背景色。 jQuery 代码: $(document.body).css( "background", "black" ); -------------------------------------------------------------------------------- 隐藏一个表单中所有元素。 jQuery 代码: $(myForm.elements).hide() jQuery(callback)jQuery(callback) $(document).ready()的简写。 允许你绑定一个在DOM文档载入完成后执行的函数。这个函数的作用如同$(document).ready()一样,只不过用这个函数时,需要把页面中所有需要在 DOM 加载完成时执行的$()操作符都包装到其中来。从技术上来说,这个函数是可链接的--但真正以这种方式链接的情况并不多。 你可以在一个页面中使用任意多个$(document).ready事件。 参考 ready(Function) 获取更多 ready 事件的信息。 -------------------------------------------------------------------------------- A shorthand for $(document).ready(). Allows you to bind a function to be executed when the DOM document has finished loading. This function behaves just like $(document).ready(), in that it should be used to wrap other $() operations on your page that depend on the DOM being ready to be operated on. While this function is, technically, chainable - there really isn't much use for chaining against it. You can have as many $(document).ready events on your page as you like. See ready(Function) for details about the ready event. 返回值 jQuery 参数 callback (Function) : 当DOM加载完成后要执行的函数 示例 当DOM加载完成后,执行其中的函数。 jQuery 代码: $(function(){ // Document is ready }); -------------------------------------------------------------------------------- Uses both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias. jQuery 代码: jQuery(function($) { // Your code using failsafe $ alias here... }); jQuery 对象访问 each(callback)each(callback) 以每一个匹配的元素作为上下文来执行一个函数。 意味着,每次执行传递进来的函数时,函数中的this关键字都指向一个不同的DOM元素(每次都是一个不同的匹配元素)。 而且,在每次执行函数时,都会给函数传递一个表示作为执行环境的元素在匹配的元素集合中所处位置的数字值作为参数(从零开始的整形)。 返回 'false' 将停止循环 (就像在普通的循环中使用 'break')。返回 'true' 跳至下一个循环(就像在普通的循环中使用'continue')。 -------------------------------------------------------------------------------- Execute a function within the context of every matched element. This means that every time the passed-in function is executed (which is once for every element matched) the 'this' keyword points to the specific DOM element. Additionally, the function, when executed, is passed a single argument representing the position of the element in the matched set (integer, zero-index). Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop). 返回值 jQuery 参数 callback (Function) : 对于每个匹配的元素所要执行的函数 示例 迭代两个图像,并设置它们的 src 属性。注意:此处 this 指代的是 DOM 对象而非 jQuery 对象。 HTML 代码: jQuery 代码: $("img").each(function(i){ this.src = "test" + i + ".jpg"; }); 结果: [ , ] -------------------------------------------------------------------------------- 如果你想得到 jQuery对象,可以使用 $(this) 函数。 jQuery 代码: $("img").each(function(){ $(this).toggleClass("example"); }); -------------------------------------------------------------------------------- 你可以使用 'return' 来提前跳出 each() 循环。 HTML 代码:
Stop here
jQuery 代码: $("button").click(function () { $("div").each(function (index, domEle) { // domEle == this $(domEle).css("backgroundColor", "yellow"); if ($(this).is("#stop")) { $("span").text("Stopped at div index #" + index); return false; } }); });size()size() jQuery 对象中元素的个数。 这个函数的返回值与 jQuery 对象的'length' 属性一致。 -------------------------------------------------------------------------------- The number of elements in the jQuery object. This returns the same number as the 'length' property of the jQuery object. 返回值 Number 示例 计算文档中所有图片数量 HTML 代码: jQuery 代码: $("img").size(); 结果: 2 lengthlength jQuery 对象中元素的个数。 当前匹配的元素个数。 size 将返回相同的值。 -------------------------------------------------------------------------------- The number of elements in the jQuery object. The number of elements currently matched. The size function will return the same value. 返回值 Number 示例 计算文档中所有图片数量 HTML 代码: jQuery 代码: $("img").length; 结果: 2 get()get() 取得所有匹配的 DOM 元素集合。 这是取得所有匹配元素的一种向后兼容的方式(不同于jQuery对象,而实际上是元素数组)。 如果你想要直接操作 DOM 对象而不是 jQuery 对象,这个函数非常有用。 -------------------------------------------------------------------------------- Access all matched DOM elements. This serves as a backwards-compatible way of accessing all matched elements (other than the jQuery object itself, which is, in fact, an array of elements). It is useful if you need to operate on the DOM elements themselves instead of using built-in jQuery functions. 返回值 Array 示例 选择文档中所有图像作为元素数组,并用数组内建的 reverse 方法将数组反向。 HTML 代码: jQuery 代码: $("img").get().reverse(); 结果: [ ] get(index)get(index) 取得其中一个匹配的元素。 num表示取得第几个匹配的元素。 这能够让你选择一个实际的DOM 元素并且对他直接操作,而不是通过 jQuery 函数。$(this).get(0)与$(this)[0]等价。 -------------------------------------------------------------------------------- Access a single matched DOM element at a specified index in the matched set. This allows you to extract the actual DOM element and operate on it directly without necessarily using jQuery functionality on it. This function called as $(this).get(0) is the equivalent of using square bracket notation on the jQuery object itself like $(this)[0]. 返回值 Element 参数 index (Number) :取得第 index 个位置上的元素 示例 HTML 代码: jQuery 代码: $("img").get(0); 结果: [ ] index(subject)index(subject) 搜索与参数表示的对象匹配的元素,并返回相应元素的索引值值。 如果找到了匹配的元素,从0开始返回;如果没有找到匹配的元素,返回-1。 -------------------------------------------------------------------------------- Searches every matched element for the object and returns the index of the element, if found, starting with zero. Returns -1 if the object wasn't found. 返回值 Number 参数 subject (Element) : 要搜索的对象 示例 返回ID值为foobar的元素的索引值值。 HTML 代码:
jQuery 代码: $("*").index($('#foobar')[0]) 结果: 5 插件机制 jQuery.fn.extend(object)jQuery.fn.extend(object) 扩展 jQuery 元素集来提供新的方法(通常用来制作插件)。 查看这里Plugins/Authoring可以获取更多信息。 -------------------------------------------------------------------------------- Extends the jQuery element set to provide new methods (used to make a typical jQuery plugin). Can be used to add functions into the to add plugin methods (plugins). 返回值 jQuery 参数 object (Object) :用来扩充 jQuery 对象。 示例 增加两个插件方法。 jQuery 代码: jQuery.fn.extend({ check: function() { return this.each(function() { this.checked = true; }); }, uncheck: function() { return this.each(function() { this.checked = false; }); } }); 结果: $("input[@type=checkbox]").check(); $("input[@type=radio]").uncheck(); jQuery.extend(object)jQuery.extend(object) 扩展jQuery对象本身。 用来在jQuery命名空间上增加新函数。 查看 'jQuery.fn.extend' 获取更多添加插件的信息。 -------------------------------------------------------------------------------- Extends the jQuery object itself. Can be used to add functions into the jQuery namespace. See 'jQuery.fn.extend' for more information on using this method to add Plugins. 返回值 jQuery 参数 object (Object) : 用以扩展 jQuery 对象 示例 在jQuery命名空间上增加两个函数。 jQuery 代码: jQuery.extend({ min: function(a, b) { return a < b ? a : b; }, max: function(a, b) { return a > b ? a : b; } }); 结果: jQuery.min(2,3); // => 2 jQuery.max(4,5); // => 5 多库共存 jQuery.noConflict()jQuery.noConflict() 运行这个函数将变量$的控制权让渡给第一个实现它的那个库。 这有助于确保jQuery不会与其他库的$对象发生冲突。在运行这个函数后,就只能使用jQuery变量访问jQuery对象。例如,在要用到$("div p")的地方,就必须换成jQuery("div p")。 -------------------------------------------------------------------------------- Run this function to give control of the $ variable back to whichever library first implemented it. This helps to make sure that jQuery doesn't conflict with the $ object of other libraries. By using this function, you will only be able to access jQuery using the 'jQuery' variable. For example, where you used to do $("div p"), you now must do jQuery("div p"). 返回值 jQuery 示例 将$引用的对象映射回原始的对象。 jQuery 代码: jQuery.noConflict(); // 使用 jQuery jQuery("div p").hide(); // 使用其他库的 $() $("content").style.display = 'none'; -------------------------------------------------------------------------------- 恢复使用别名$,然后创建并执行一个函数,在这个函数的作用域中仍然将$作为jQuery的别名来使用。在这个函数中,原来的$对象是无效的。这个函数对于大多数不依赖于其他库的插件都十分有效。 jQuery 代码: jQuery.noConflict(); (function($) { $(function() { // 使用 $ 作为 jQuery 别名的代码 }); })(jQuery); // 其他用 $ 作为别名的库的代码 -------------------------------------------------------------------------------- 创建一个新的别名用以在接下来的库中使用jQuery对象。 jQuery 代码: var j = jQuery.noConflict(); // 基于 jQuery 的代码 j("div p").hide(); // 基于其他库的 $() 代码 $("content").style.display = 'none'; jQuery.noConflict(extreme)jQuery.noConflict(extreme) 将$和jQuery的控制权都交还给原来的库。用之前请考虑清楚! 这是相对于简单的 noConflict 方法更极端的版本,因为这将完全重新定义jQuery。这通常用于一种极端的情况,比如你想要将jQuery嵌入一个高度冲突的环境。注意:调用此方法后极有可能导致插件失效。 -------------------------------------------------------------------------------- Revert control of both the $ and jQuery variables to their original owners. Use with discretion. This is a more-extreme version of the simple noConflict method, as this one will completely undo what jQuery has introduced. This is to be used in an extreme case where you'd like to embed jQuery into a high-conflict environment. NOTE: It's very likely that plugins won't work after this particular method has been called. 返回值 jQuery 参数 extreme (Boolean) : 传入 true 来允许彻底将jQuery变量还原 示例 完全将 jQuery 移到一个新的命名空间。 jQuery 代码: var dom = {}; dom.query = jQuery.noConflict(true); 结果: // 新 jQuery 的代码 dom.query("div p").hide(); // 另一个库 $() 的代码 $("content").style.display = 'none'; // 另一个版本 jQuery 的代码 jQuery("div > p").hide(); 选择器基本 #id#id 根据给定的ID匹配一个元素。 -------------------------------------------------------------------------------- Matches a single element with the given id attribute. 返回值 Element 参数 id (String) : 用于搜索的,通过元素的 id 属性中给定的值 示例 查找 ID 为"myDiv"的元素。 HTML 代码:

id="notMe"

id="myDiv"
jQuery 代码: $("#myDiv"); 结果: [
id="myDiv"
] elementelement 根据给定的元素名匹配所有元素 -------------------------------------------------------------------------------- Matches all elements with the given name. 返回值 Array 参数 element (String) : 一个用于搜索的元素。指向 DOM 节点的标签名。 示例 查找一个 DIV 元素。 HTML 代码:
DIV1
DIV2
SPAN jQuery 代码: $("div"); 结果: [
DIV1
,
DIV2
] .class.class 根据给定的类匹配元素。 -------------------------------------------------------------------------------- Matches all elements with the given class. 返回值 Array 参数 class (String) : 一个用以搜索的类。一个元素可以有多个类,只要有一个符合就能被匹配到。 示例 查找所有类是 "myClass" 的元素. HTML 代码:
div class="notMe"
div class="myClass"
span class="myClass" jQuery 代码: $(".myClass"); 结果: [
div class="myClass"
, span class="myClass" ] ** 匹配所有元素 多用于结合上下文来搜索。 -------------------------------------------------------------------------------- Matches all elements. Most useful when combined with a context to search in. 返回值 Array 示例 找到每一个元素 HTML 代码:
DIV
SPAN

P

jQuery 代码: $("*") 结果: [
DIV
, SPAN,

P

] selector1,selector2,selectorNselector1,selector2,selectorN 将每一个选择器匹配到的元素合并后一起返回。 你可以指定任意多个选择器,并将匹配到的元素合并到一个结果内。 -------------------------------------------------------------------------------- Matches the combined results of all the specified selectors. You can specify any number of selectors to combine into a single result. 返回值 Array 参数 selector1 (Selector) : 一个有效的选择器 selector2 (Selector) : 另一个有效的选择器 selectorN (Selector) : (可选) 任意多个有效选择器 示例 找到匹配任意一个类的元素。 HTML 代码:
div

p class="myClass"

span

p class="notMyClass"

jQuery 代码: $("div,span,p.myClass") 结果: [
div
,

p class="myClass"

, span ] 层级 ancestor descendantancestor descendant 在给定的祖先元素下匹配所有的后代元素 -------------------------------------------------------------------------------- Matches all descendant elements specified by descendant of elements specified by ancestor. 返回值 Array 参数 ancestor (Selector) : 任何有效选择器 descendant (Selector) : 用以匹配元素的选择器,并且它是第一个选择器的后代元素 示例 找到表单中所有的 input 元素 HTML 代码:
jQuery 代码: $("form input") 结果: [ , ] parent > childparent > child 在给定的父元素下匹配所有的子元素 -------------------------------------------------------------------------------- Matches all child elements specified by child of elements specified by parent. 返回值 Array 参数 parent (Selector) : 任何有效选择器 child (Selector) : 用以匹配元素的选择器,并且它是第一个选择器的子元素 示例 匹配表单中所有的子级input元素。 HTML 代码:
jQuery 代码: $("form > input") 结果: [ ] prev + nextprev + next 匹配所有紧接在 prev 元素后的 next 元素 -------------------------------------------------------------------------------- Matches all next elements specified by next that are next to elements specified by prev. 返回值 Array 参数 prev (Selector) : 任何有效选择器 next (Selector) :一个有效选择器并且紧接着第一个选择器 示例 匹配所有跟在 label 后面的 input 元素 HTML 代码:
jQuery 代码: $("label + input") 结果: [ , ] prev ~ siblingsprev ~ siblings 匹配 prev 元素之后的所有 siblings 元素 -------------------------------------------------------------------------------- Matches all sibling elements after the "prev" element that match the filtering "siblings" selector. 返回值 Array 参数 prev (Selector) : 任何有效选择器 siblings (Selector) : 一个选择器,并且它作为第一个选择器的同辈 示例 找到所有与表单同辈的 input 元素 HTML 代码:
jQuery 代码: $("form ~ input") 结果: [ ] 简单 :first:first 匹配找到的第一个元素 -------------------------------------------------------------------------------- Matches the first selected element. 返回值 Element 示例 查找表格的第一行 HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:first") 结果: [ Header 1 ] :last:last 匹配找到的最后一个元素 -------------------------------------------------------------------------------- Matches the last selected element. 返回值 Element 示例 查找表格的最后一行 HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:last") 结果: [ Value 2 ] :not(selector):not(selector) 去除所有与给定选择器匹配的元素 -------------------------------------------------------------------------------- Removes all elements matching the given selector. 返回值 Array 参数 selector (Selector) : 用于筛选的选择器 示例 查找所有未选中的 input 元素 HTML 代码: jQuery 代码: $("input:not(:checked)") 结果: [ ] :even:even 匹配所有索引值为偶数的元素,从 0 开始计数 -------------------------------------------------------------------------------- Matches even elements, zero-indexed. 返回值 Array 示例 查找表格的1、3、5...行(即索引值0、2、4...) HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:even") 结果: [ Header 1, Value 2 ] :odd:odd 匹配所有索引值为奇数的元素,从 0 开始计数 -------------------------------------------------------------------------------- Matches odd elements, zero-indexed. 返回值 Array 示例 查找表格的2、4、6行(即索引值1、3、5...) HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:odd") 结果: [ Value 1 ] :eq(index):eq(index) 匹配一个给定索引值的元素 -------------------------------------------------------------------------------- Matches a single element by its index. 返回值 Element 参数 index (Number) : 从 0 开始计数 示例 查找第二行 HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:eq(1)") 结果: [ Value 1 ] :gt(index):gt(index) 匹配所有大于给定索引值的元素 -------------------------------------------------------------------------------- Matches all elements with an index above the given one. 返回值 Array 参数 index (Number) : 从 0 开始计数 示例 查找第二第三行,即索引值是1和2,也就是比0大 HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:gt(0)") 结果: [ Value 1, Value 2 ] :lt(index):lt(index) 匹配所有小于给定索引值的元素 -------------------------------------------------------------------------------- Matches all elements with an index below the given one. 返回值 Array 参数 index (Number) : 从 0 开始计数 示例 查找第一第二行,即索引值是0和1,也就是比2小 HTML 代码:
Header 1
Value 1
Value 2
jQuery 代码: $("tr:lt(2)") 结果: [ Header 1, Value 1 ] :header:header 匹配如 h1, h2, h3之类的标题元素 -------------------------------------------------------------------------------- Matches all elements that are headers, like h1, h2, h3 and so on. 返回值 Array 示例 给页面内所有标题加上背景色 HTML 代码:

Header 1

Contents 1

Header 2

Contents 2

jQuery 代码: $(":header").css("background", "#EEE"); 结果: [

Header 1

,

Header 2

] :animated:animated 匹配所有没有在执行动画效果中的元素 -------------------------------------------------------------------------------- Matches all elements that are currently being animated. 返回值 Array 示例 只有对不在执行动画效果的元素执行一个动画特效 HTML 代码:
jQuery 代码: $("#run").click(function(){ $("div:not(:animated)").animate({ left: "+20" }, 1000); }); 内容 :contains(text):contains(text) 匹配包含给定文本的元素 -------------------------------------------------------------------------------- Matches elements which contain the given text. 返回值 Array 参数 text (String) : 一个用以查找的字符串 示例 查找所有包含 "John" 的 div 元素 HTML 代码:
John Resig
George Martin
Malcom John Sinclair
J. Ohn jQuery 代码: $("div:contains('John')") 结果: [
John Resig
,
Malcom John Sinclair
] :empty:empty 匹配所有不包含子元素或者文本的空元素 -------------------------------------------------------------------------------- Matches all elements that are empty, be it elements or text. 返回值 Array 示例 查找所有不包含子元素或者文本的空元素 HTML 代码:
Value 1
Value 2
jQuery 代码: $("td:empty") 结果: [ , ] :has(selector):has(selector) 匹配含有选择器所匹配的元素的元素 -------------------------------------------------------------------------------- Matches elements which contain at least one element that matches the specified selector. 返回值 Array 参数 selector (Selector) : 一个用于筛选的选择器 示例 给所有包含 p 元素的 div 元素添加一个 text 类 HTML 代码:

Hello

Hello again!
jQuery 代码: $("div:has(p)").addClass("test"); 结果: [

Hello

] :parent:parent 匹配含有子元素或者文本的元素 -------------------------------------------------------------------------------- Matches all elements that are parents - they have child elements, including text. 返回值 Array 示例 查找所有含有子元素或者文本的 td 元素 HTML 代码:
Value 1
Value 2
jQuery 代码: $("td:parent") 结果: [ Value 1, Value 1 ] 可见性 :hidden:hidden 匹配所有的不可见元素,input 元素的 type 属性为 "hidden" 的话也会被匹配到 -------------------------------------------------------------------------------- Matches all elements that are hidden, or input elements of type "hidden". 返回值 Array 示例 查找所有不可见的 tr 元素 HTML 代码:
Value 1
Value 2
jQuery 代码: $("tr:hidden") 结果: [ Value 1 ] :visible:visible 匹配所有的可见元素 -------------------------------------------------------------------------------- Matches all elements that are visible. 返回值 Array 示例 查找所有可见的 tr 元素 HTML 代码:
Value 1
Value 2
jQuery 代码: $("tr:visible") 结果: [ Value 2 ] 属性 [attribute][attribute] 匹配包含给定属性的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute. 返回值 Array 参数 attribute (String) : 属性名 示例 查找所有含有 id 属性的 div 元素 HTML 代码:

Hello!

jQuery 代码: $("div[id]") 结果: [
] [attribute=value][attribute=value] 匹配给定的属性是某个特定值的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute with a certain value. 返回值 Array 参数 attribute (String) : 属性名 value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 属性是 newsletter 的 input 元素 HTML 代码: ' jQuery 代码: $("input[name='newsletter']").attr("checked", true); 结果: [ , ] [attribute!=value][attribute!=value] 匹配给定的属性是不包含某个特定值的元素 -------------------------------------------------------------------------------- Matches elements that don't have the specified attribute with a certain value. 返回值 Array 参数 attribute (String) : 属性名 value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 属性不是 newsletter 的 input 元素 HTML 代码: ' jQuery 代码: $("input[name!='newsletter']").attr("checked", true); 结果: [ ] [attribute^=value][attribute^=value] 匹配给定的属性是以某些值开始的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute and it starts with a certain value. 返回值 Array 参数 attribute (String) : 属性名 value ( String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 以 'news' 开始的 input 元素 HTML 代码: jQuery 代码: $("input[name^='news']") 结果: [ , ] [attribute$=value][attribute$=value] 匹配给定的属性是以某些值结尾的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute and it ends with a certain value. 返回值 Array 参数 attribute (String) : 属性名 value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 以 'letter' 结尾的 input 元素 HTML 代码: jQuery 代码: $("input[name$='letter']") 结果: [ , ] [attribute*=value][attribute*=value] 匹配给定的属性是以包含某些值的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute and it contains a certain value. 返回值 Array 参数 attribute (String) : 属性名 value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 包含 'man' 的 input 元素 HTML 代码: jQuery 代码: $("input[name*='man']") 结果: [ , , ] [selector1][selector2][selectorN][selector1][selector2][selectorN] 复合属性选择器,需要同时满足多个条件时使用。 -------------------------------------------------------------------------------- Matches elements that have the specified attribute and it contains a certain value. 返回值 Array 参数 selector1 (Selector) : 属性选择器 selector2 (Selector) : 另一个属性选择器,用以进一步缩小范围 selectorN (Selector) : 任意多个属性选择器 示例 找到所有含有 id 属性,并且它的 name 属性是以 man 结尾的 HTML 代码: jQuery 代码: $("input[id][name$='man']") 结果: [ ] 子元素 :nth-child(index/even/odd/equation):nth-child(index/even/odd/equation) 匹配其父元素下的第N个子或奇偶元素 ':eq(index)' 只匹配一个元素,而这个将为每一个父元素匹配子元素。:nth-child从1开始的,而:eq()是从0算起的! 可以使用: nth-child(even) :nth-child(odd) :nth-child(3n) :nth-child(2) :nth-child(3n+1) :nth-child(3n+2) -------------------------------------------------------------------------------- Matches the nth-child of its parent. While ':eq(index)' matches only a single element, this matches more then one: One for each parent. The specified index is one-indexed, in contrast to :eq() which starst at zero. 返回值 Array 参数 index (Number) : 要匹配元素的序号,从1开始 示例 在每个 ul 查找第 2 个li HTML 代码:
  • John
  • Karl
  • Brandon
  • Glen
  • Tane
  • Ralph
jQuery 代码: $("ul li:nth-child(2)") 结果: [
  • Karl
  • ,
  • Tane
  • ] :first-child:first-child 匹配第一个子元素 ':first' 只匹配一个元素,而此选择符将为每个父元素匹配一个子元素 -------------------------------------------------------------------------------- Matches the first child of its parent. While ':first' matches only a single element, this matches more then one: One for each parent. 返回值 Array 示例 在每个 ul 中查找第一个 li HTML 代码:
    • John
    • Karl
    • Brandon
    • Glen
    • Tane
    • Ralph
    jQuery 代码: $("ul li:first-child") 结果: [
  • John
  • ,
  • Glen
  • ] :last-child:last-child 匹配最后一个子元素 ':last'只匹配一个元素,而此选择符将为每个父元素匹配一个子元素 -------------------------------------------------------------------------------- Matches the last child of its parent. While ':last' matches only a single element, this matches more then one: One for each parent. 返回值 Array 示例 在每个 ul 中查找最后一个 li HTML 代码:
    • John
    • Karl
    • Brandon
    • Glen
    • Tane
    • Ralph
    jQuery 代码: $("ul li:last-child") 结果: [
  • Brandon
  • ,
  • Ralph
  • ] :only-child:only-child 如果某个元素是父元素中唯一的子元素,那将会被匹配 如果父元素中含有其他元素,那将不会被匹配。 -------------------------------------------------------------------------------- Matches the only child of its parent. If the parent has other child elements, nothing is matched. 返回值 Array 示例 在 ul 中查找是唯一子元素的 li HTML 代码:
    • John
    • Karl
    • Brandon
    JavaScript:定义行为和动作 (基于对象和事件驱动的客户端脚本语言;也是一种广泛应用于客户端Web开发的脚本语言) 基于对象:网页中的一切元素都是假象!不需要new,即可直接使用 事件驱动:JavaScript的执行都是由事件引发 解释执行:先读到的先执行,后读到的会替代先读的 可以使用任何文本编辑工具编写 JavaScript 代码,然后由浏览器解释执行。 JavaScript常用于实现如下功能: |--控制文档的外观和内容; |--对浏览器的控制; |--与 HTML 表单的交互; |--与用户的交互; |--执行计算等。 1.单击事件:定义在按钮的开始标签中 语法:onclick="js语句" 弹出警告:alert(‘字符串’) 强调:1.js区分大小写 2.字符串单双引号不区分 [removed]标签:页面中专门集中编写JavaScript的区域 js的方法定义:function方法名([参数列表]){ 方法体 [return 返回值] } 注意:js中的方法可以直接写在代码中,不需要“类”包裹 使用方法:方法名()-->方法调用-->立即执行 2.js文件:网页外专门保存js脚本的文件--推荐 强调:HTML、CSS、JS都要使用UTF-8编码保存(window系统) 使用js文件引入网页:[removed][removed] 强调:一旦定义src属性则其中的代码失效 解释执行:语句也可以直接写在js文件中,边解释边执行 3.***调试*** |--①.只有在执行时,才会报错 |--②.错误信息,浏览器页面看不到--没效果 | 解决:控制台--(工具-->JavaScript控制台或F12) | 包含错误信息;指向错误位置的超链接 |--③.打桩:在指定变量位置输出变量或对象的内容 console.log(内容);-->出现在控制台-->日志 4.变量:js中的变量不需要提前指定类型,由赋值时动态决定 所有的变量都用var声明 5.数据类型: |--String(字符串类型) |--Number(数字类型) |--Boolean(布尔类型)0、-0、null、""、false/undefined或NaN,则该对象设置为false。其余都可以当true 6.数据类型的隐式转换: |--数字 + 字符串:数字转换为字符串 |--数字 + 布尔值:true转换为1,false转换为0 |--字符串 + 布尔值:布尔值转换为字符串true或false |--布尔值 + 布尔值:布尔值转换为数值1或0 7.数据类型转换函数 :(方法前不需要对象调用的:全局函数) |--toString():转换成字符串。所有数据类型均可转换为 string 类型; |--parseInt():强制转换成整数。如果不能转换,则返回 NaN(not a number); |--parseFloat():强制转换成浮点数。如果不能转换,则返回 NaN(Not a Number) |--typeof():查询数值当前类型。 |--isNaN():判断是否为数字。返回 true:不是数字/false:是数字 | |--isNan(""):对空字符串不验证,直接返回false | |--如果输入的是字符串类型的数字,返回false--不能判断数据类型,只判断内容 | |--如果输入的是boolean,返回false。因为boolean可以和number类型直接做计算 | |--和任何数字计算都得NaN;和任何数字作比较都得false |--注:[removed]();节点输出,即在当前位置输出括号里的内容 *凡是从页面上进入js的都是字符串类型 8.查找元素:抓住根节点,就等于抓住整棵树 网页的根节点:document对象 要找元素,必须利用document对象(当前网页文件) 精确查找某个ID的元素:document.getElementById(id名); 9.null和undefined: |--null:一个特殊的值,表示“无值”--空对象。数据类型为Object |--undefined:表示声明了变量但从未赋值或者对象属性不存在 10.双等号(==)和全等号(===): |--“==”为确定两个运算数是否相等,“==”运算符会进行类型转换。转换后运算数相等就返回true,否则返回false |--“===”不执行类型转换,即,只有在无需类型转换运算数就相等的情况下,才返回true,否则返回false |--附:if(null)、if(defined)、if(NaN)都相当于if(false) -----猜数字游戏----- 失去焦点时,判断猜对猜错 获得焦点:onfocus 失去焦点:onblur 当事件就发生在获得内容的当前元素上: this直接获得当前内容对象。可以代替当前对象完成一切操作,拥有当前对象的所有属性 前端优化:js中最好用三目运算代替if else 11.String: |--查找:x.indexOf(‘关键字’[,开始位置下标]) | 每次只查找第一次出现的位置 |--替换:x.replace(‘关键字’[,‘替换内容’])--不会修改原x字符串 | 每次只替换第一次找到的 |--查找和替换所有:while循环 | |--String 对象的常用方法有: |--x.toLowerCase()、x.toUpperCase():大小写转换方法; |--x.charAt(index):返回指定位置的字符; |--x.charCodeAt(index):返回指定位置字符的Unicode编码; |--x.indexOf(findstr,index)、x.lastIndexOf(findstr,index): 获取指定字符; |--x.substring(start, end): 获取子字符串; |--x.replace(findstr,tostr):替换子字符串; |--x.split(bystr): 拆分子字符串。 |--String与正则表达式 |--str.match(regExp);--查找str中匹配正则表达式的关键字 | 返回:如果没找到,返回null。若找到,返回1个数组,数组的每个元素是每个找到的匹配关键 | |--str.replace(regExp,"替换值")--替换str中所有匹配的关键字 | 强调:replace方法不改变元字符串,只能返回新字符串。必须用变量接收新字符串 | |--js中正则表达式语法:/正则表达式/[属性后缀]--其中属性后缀,g:全局匹配 i:忽略大小写(仅英文有效) 如果不适用模式匹配方式,将执行原文匹配 结果:如果正则表达式写错,也将执行原文匹配 12.Array笔试题:js中数组声明方式: A new Array(7) B new Array(7,‘a’,true) C [7,'a',true]--js中所有[]都表示数组 D []--实例化一个空数组对象 实例化空数组:var arr=[]; 特点:元素个数不限定,元素类型不限制 13.Array 对象的常用方法: |--1.join()方法--用于把数组中的所有元素放入一个字符串   | eparato表示要使用的分隔符。如果省略该参数,则使用逗号作为分隔符 |--2.toString()方法--可把数组转换为字符串,并返回结果 |--3.concat()方法--用于连接两个或多个数组,该方法不会改变现有的数组 |--4.slice()方法--截取元素。根据给定的范围可从已有的数组中返回选定的元素 14.Function:js中一切都是对象,连方法都是1个对象!! 笔试题:js中方法定义集中方式: A:function compare(a,b){return a-b;}---*可以任意地方声明方法* B:var compare=function(a,b){return a-b;} ---| --其实方法名也是方法对象的变量名;等号右边其实就是1个匿名方法对象 |--*必须在传递之前声明方法对象* C:var compare=new Function('a','b','return a-b') ---| --其实js底层就是new Function;构造函数中的参数都是字符串 结论:所有的方法都是function类型的。 15.JavaScript中的所有事物都是对象,分为三类: |--简单对象:String、Number、Boolean |--组合对象:Array、Math、Date |--复杂对象:Function、Regex、Object等 16.Array排序: |--升序:function compare(a,b){return a-b;} |--降序:function compare(a,b){return b-a;} 比较器用法:arr.sort(比较器方法名); 遍历:for(var i=0;i属性: |--document:窗口中显示的 HTML 文档对象 |--history:本次浏览过窗口的历史记录 | --前进:history.go(1);后退:history.go(-1);刷新:history.go(0); |--location:窗口文件地址对象(地址栏) |--event:事件对象 |--screen:屏幕对象 |--name:窗口名称 |--opener:打开当前窗口的 window 对象 |--navigator:有关浏览器的信息 |--cookieEnable:判断当前浏览器是否启用cookie |--userAgent:获得浏览器的名称和版本号 window 对象的常用方法有: |--alert();--警告框--只能点确认 |--confirm();确认框--可以选择确认或取消 |--prompt();对话框--用于显示可提示用户进行输入 |--window.open('url'[,'name']):打开1个选项卡 |--window.close():关闭当前选项卡 24.定时器:凡是网页中自动动态的效果都是用计时器实现的 |--周期性定时器:每隔一个时间段自动执行一次,循环执行 | |--setInterval() 启动-->用于启动一个周期性定时器 | 语法:timer=setInterval(方法名,间隔毫秒数) |--clearInterval() 停止-->用于停止一个周期性定时器 语法:timer=clearInterval(timer) |--1次性定时器:先等待一段时间,再自动执行一次,自动结束 |--setTimeout() 启动-->用于启动一个一次性定时器 |--clearTimeout() 停止-->用于停止一个一次性定时器 附: |--表单控件,读写内容:.value |--普通html元素,要想读写开始标签和结束标签之间的内容:[removed] 计时器:1.做什么事:方法; 2.一个变量:存计时器的线程号 3.何时启动计时器:事件 25.document:1.代表当前网页文档; 2.所有网页元素的根元素; 3.查找和操作元素,都要依靠document; 节点:nodeName--标签名或属性名 DOM操作样式:对象.className属性 等效于<标签class=""> DOM树:当前节点向上:txtObj[removed]() 向下:txtObj.getElementByTagName() 26.表单中查找控件对象:document.getElementsByName('name属性') DOM增加新节点:两种方法 创建新节点:document.creatElement('标签名') --此时新元素对象仅在内存中游离,页面看不见! 添加新节点:找到父元素! |--appendChild() 方法只能将新节点作为某节点的最后一个子节点 | --此时新元素追加到了父元素的末尾 |--parentNode.insertBefore()(新元素对象,A)--其中A为新元素要插入位置的后一位元素 DOM删除元素:node.removeChild(childNode)--node为父节点,childNode为要删除的节点 27.HTML DOM SelectOption对象: select对象:代表HTML表单中的一个下拉列表,每个<select> 标签即表示一个 Select 对象 |--属性: |--options:返回包含<select>元素中所有<option>的一个数组,每个元素对应一个<option>标签,索引从0开始 |--selectedIndex:设置或返回下拉列表中被选选项的索引号 |--size:设置或返回下拉列表中一次显示显示的选项数 |--方法: |--add(option):用于向<select>添加一个<option>元素。 |--remove(index):从下拉列表删除选项 Select对象有事件onChange当改变选择时调用的事件句柄。Select对象的onChange属性引用了一个事件句柄函数。当用户选 中一个选项,或者取消了对一个选项的选定时,就会调用该句柄。这个事件不会指定新的选项是什么,必须通过Select对象的 selectedIndex属性,或者各个Option对象的selected属性来确定这一点。 option对象:代表HTML表单中下拉列表中的一个选项,每个<option>标签表示一个Option对象 |--创建:var o = new Option(text,value); |--属性: |--index:返回下拉列表中选项的索引位置 |--text:设置或返回选项的文本值; |--value:设置或返回选项的值; |--selected:设置或返回选项的 selected 属性的值,该属性设置选项的当前状态,如果为 true,则该选项被选中 28.table对象: rows数组:表中所有行对象 方法:var row=table.iusertRow(i);--返回刚添加的新行 table.deleteRow(i); --i:行的下标。如果i取-1,即在表格末尾追加一行 tableRow对象: cells数组:当前行中所有单元格的对象 方法:var 刚添加的新单元格=tr.iusertCell(i); tr.deleteCell(i); tableCell对象:取单元格内容--td[removed] 29.事件冒泡: |--1.由外向内,捕获事件:记录哪级元素有什么事件 |--2.由内向外,冒泡执行:按记录的顺序由内向外执行 取消冒泡: |--1.获得事件对象!--event onclick="func(event)"--其中event和this都是关键字,表示事件对象 func(e)--此时e就是事件对象 |--2.取消冒泡:e.cancelBubble=true; 何时用:如果大量子元素拥有相同事件,应将相同事件统一定义在1个父元素上1次即可

    87,907

    社区成员

    发帖
    与我相关
    我的任务
    社区描述
    Web 开发 JavaScript
    社区管理员
    • JavaScript
    • 无·法
    加入社区
    • 近7日
    • 近30日
    • 至今
    社区公告
    暂无公告

    试试用AI创作助手写篇文章吧