您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

简洁易懂的jQuery:jQuery操作

2024/6/4 9:14:06发布28次查看
动态创建、操作和添加 html您可以通过向 jquery 函数传递原始 html 字符串来动态创建 html 标记。
<!doctype html><html lang=en><body> <script src=https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script> <script> (function($){ alert($('<div><a></a></div>').get(0).nodename); // alerts div alert($('<div><a></a></div>').length); // alerts 1 <div> is in the wrapper set alert($('<div><a></a></div><div><a></a></div>').length); // alerts 2 <div> are in the set })(jquery); </script></body></html>
需要注意的是,使用 jquery 函数创建 dom 结构时,只有结构中的根元素会添加到包装器集中。在前面的代码示例中, 元素将是包装器集中的唯一元素。
一旦创建了 html 结构中的任何元素,我们就可以使用 find() 方法对其进行操作。
<!doctype html><html lang=en><body> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script> <script> (function ($) { $('<div><a></a></div>') .find('a') .text('jquery') .attr('href', 'http://www.jquery.com'); })(jquery); </script></body></html>
对新创建的 html 进行操作后,还可以使用 jquery 的操作方法之一将其添加到 dom 中。下面我们使用 appendto() 方法将标记添加到页面。
<!doctype html><html lang=en><body> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script> <script> (function($){ $('<div><a></a></div>') .find('a') .text('jquery') .attr('href', 'http://www.jquery.com') .end().appendto('body'); // end() is used to exit the find() method })(jquery); </script></body></html>
注释:不包含属性的简单元素 - 例如$('
') - 通过 document.createelement dom 方法创建,而所有其他情况都依赖于 innerhtml 属性。事实上,您可以直接向 jquery 函数传递使用 document.createelement -e.g 创建的元素。 $(document.createelement('div'))。
传递给 jquery 的 html 字符串不能包含在 元素内可能被视为无效的元素。
传递给 jquery 函数的 html 字符串必须格式正确。
传递 jquery html 时,您应该打开和关闭所有 html 元素。不这样做可能会导致错误,主要是在 internet explorer 中。为了安全起见,请始终关闭 html 元素并避免编写快捷 html - 例如$().
摸索index()方法您可以通过将元素传递给 index() 方法来确定包装集中元素的索引。例如,假设您有一个包含网页中所有 元素的包装集,并且您想知道最后一个 元素的索引。
<!doctype html><html lang=en><body> <div>0</div> <div>1</div> <div>2</div> <div>3</div> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script> <script> (function ($) { // alerts 3 alert($('div').index($('div:last'))); })(jquery); </script></body></html>
index() 的使用并没有真正击中要害,直到我们考虑如何将它与事件一起使用。例如,通过点击下面代码中的 元素,我们可以将点击的 元素(使用关键字 this)传递给 index() 方法来确定点击的 的索引。
<!doctype html><html lang=en><body> <div id=nav> <div>nav text</div> <div>nav text</div> <div>nav text</div> <div>nav text</div> </div> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script> <script> (function ($) { // alert index of the clicked div amongst all div's in the wrapper set $('#nav div').click(function () { alert($('#nav div').index(this)); // or, a nice trick... alert($(this).prevall().length); }); })(jquery); </script></body></html>
摸索 text() 方法人们可能会错误地认为 text() 方法仅返回包装器集中第一个元素的文本节点。但是,它实际上会连接包装器集中包含的所有元素的文本节点,然后将连接后的值作为单个字符串返回。确保您了解此功能,否则您可能会得到一些意想不到的结果。
<!doctype html><html lang=en><body> <div>1,</div> <div>2,</div> <div>3,</div> <div>4</div> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script> <script> (function ($) { alert($('div').text()); // alerts 1,2,3,4 })(jquery); </script></body></html>
使用正则表达式更新或删除字符使用 javascript replace() 方法结合一些 jquery 功能,我们可以非常轻松地更新或删除元素中包含的文本中的任何字符模式。
<!doctype html><html lang=en><body> <p> i really hate using javascript. i mean really hate it! it is the best twister ever! </p> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script> <script> (function ($) {var $p = $('p'); // replace 'hate' with 'love' $p.text($p.text().replace(/hate/ig, 'love')); // remove 'twister' and replace it with nothing $p.text($p.text().replace(/twister/ig, '')); // keep in mind that text() returns a string, not the jquery object. // that is how the replace() string method is chained after using text() })(jquery); </script></body></html>
您还可以更新从 html() 返回的字符串中包含的任何字符。这意味着您不仅可以更新文本,还可以通过正则表达式更新和替换 dom 元素。
摸索 .contents() 方法.contents() 方法可用于查找所有子元素节点,包括元素内部包含的文本节点。然而,有一个问题。如果检索到的内容仅包含文本节点,则所选内容将作为单个文本节点放置在包装器集中。但是,如果您要检索的内容在文本节点中包含一个或多个元素节点,则 .contents() 方法将包含文本节点和元素节点。检查下面的代码以掌握这个概念。
<!doctype html><html lang=en><body> <p>i love using jquery!</p> <p>i love <strong>really</strong> using jquery!</p> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script> <script> (function ($) { // alerts i love using jquery! because no html elements exist alert($('p:first').contents().get(0).nodevalue); // alerts i love alert($('p:last').contents().get(0).nodevalue); // alerts really but is an html element, not a text node alert($('p:last').contents().eq(1).text()); // alerts using jquery! alert($('p:last').contents().get(2).nodevalue); })(jquery); </script></body></html>
请注意,当包装集中的项目是文本节点时,我们必须使用 .get(0).nodevalue 提取值。 contents() 方法对于提取文本节点值很方便。可以使用 contents() 从 dom 结构中仅提取文本节点。
<!doctype html><html lang=en><body> <p>jquery gives me <strong>more <span>power</span></strong> than any other web tool! </p> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script> <script> (function($){ $('p') .find('*') // select all nodes .andself() // include <p> .contents() // grab all child nodes, including text .filter(function() {return this.nodetype == node.text_node;}) // remove non-text nodes .each(function (i, text) { alert(text.nodevalue) }); // alert text contained in wrapper set })(jquery); </script></body></html>
使用remove()不会从包装集中删除元素当您使用 remove() 时,来自 dom 的 dom 片段已删除的 dom 结构中包含的元素仍然包含在包装集中。您可以删除一个元素,对该元素进行操作,然后将该元素实际放回到 dom 中,所有这些都在单个 jquery 链中。
<!doctype html><html lang=en><body> <div>remove me</div> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script> <script> (function ($) { $('div') .remove().html('<a href=http://www.jquery.com>jquery</a>') .appendto('body'); })(jquery); </script></body></html>
这里的要点是,仅仅因为您 remove() 元素并不意味着它们已从 jquery 包装器集中删除。
以上就是简洁易懂的jquery:jquery操作的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product