jQuery - Modifying HTML Elements

Adding and Removing HTML Elements

Dynamically adding elements to our HTML page is a powerful tool—it lets us modify not only the formatting, but the actual structure of our websites in response to a user‘s actions.

.append() inserts the specified element as the last child of the target element.  // $(".info").append("<p>Stuff!</p>");

.prepend() inserts the specified elements as the first child of the target element. //$(".info").prepend("<p>Stuff!</p>");

.appendTo() does the same as .append(), $(‘<p>stuff!</p>‘).appendTo(‘.info‘);

.prepend() does the same as .prepend()

We can specify where in the DOM we insert an element with the .before() and .after() functions.

$(‘target‘).after(‘<tag>to add</tag>‘);

var $paragraph = $("p"); //existing elements
$("div").after($paragraph); //move it

//same as:
$("div").after($("p"));

Note: This does not copy the element from one location to another, it moves the original element effectively saving you from having to delete the original

.empty() deletes an element‘s content and all its descendants.

.remove() not only deletes an element‘s content, but deletes the element itself.

Modifying Classes and Content

.addClass() and .removeClass() can used to add or remove a class from an element.

$(‘selector‘).addClass(‘className‘);
$(‘selector‘).removeClass(‘className‘);

.toggleClass() function that does exactly this. If the element it‘s called on has the class it receives as an input, .toggleClass() removes that class; if the target element doesn‘t have that class, .toggleClass() adds

$("div").css("background-color","#008800");  //change css style

.html() and .val can update contents of our HTML elements.

$(‘div‘).html("I love jQuery!");

.val() is used to get the value of from elements.

$(‘input:checkbox:checked‘).val();

时间: 2024-10-14 12:04:40

jQuery - Modifying HTML Elements的相关文章

jquery.pjax.js bug问题解决集锦

jquery.pjax 是一个很好的局部刷新插件,但实际应用过程是还是会有很多小问题,部分问题解决如下: 1.pjax 局部加载时候,IE 存在缓存问题,很容易理解,pjax是通过jquery的ajax加载局部内容的,默认cache=true,这会导致ie下get数据从缓存中获取,解决办法是设置pjax options的cache=false,这样请求会自动变成如下方式: /XXXX?_pjax=%23pjax-container&_=1455092848927 2.pjax 与 jquery

jQuery源码

/*! * jQuery JavaScript Library v1.8.3 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright 2012 jQuery Foundation and other contributors * Released under the MIT license * http://jquery.org/license * * Date: Tue Nov 13 20

Jquery之事件绑定(bind(),live(),delegate(),on())

1..bind() 描述: 为一个元素绑定一个事件处理程序. .bind()一个基本的用法: $(selector).bind('click', function() { alert('User clicked on "foo."'); }); 可以直接用原生js替代为: $(selector).click( function() { alert('User clicked on "foo."'); }); 在jQuery1.4.3,您现在可以通过传递false代替

jquery 2.1.0 源码

/*! * jQuery JavaScript Library v2.1.0 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright 2005, 2014 jQuery Foundation, Inc. and other contributors * Released under the MIT license * http://jquery.org/license * * Date: 2

jQuery源码中的Ajax--serialize()/serializeArray()/param()方法

由于jQueryObject.serialize()方法的核心是$.param()方法,所以先学习$.param()方法. 一.$.param()方法 $.param()方法是用来对一个数组或对象按照key/value进行序列化,以便用于URL查询字符串或AJAX请求.其返回的字符串已经过URL编码处理(采用的字符集为UTF-8). 语法: jQuery.param( obj [, traditional ] ) 参数如下: 参数 描述 obj 需要被序列化的JS对象. traditional

JQuery日记 2014-04.29

JQuery原型对象(续) (4).each(callback,args) 迭代JQuery对象上的元素并调用callback参数,如果某次迭代中callback返回false,停止迭代.args为callback的参数,args必须为数组.在callback中this引用的当前迭代的元素. Test_HTML <body> <div id= "div1">div1</div ><div id= "div2">div2

jquery源码10-提交的数据和ajax()

{ var r20 = /%20/g, //全部空格 rbracket = /\[\]$/, //结尾位置匹配中括号 rCRLF = /\r?\n/g, rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i, rsubmittable = /^(?:input|select|textarea|keygen)/i; jQuery.fn.extend({ serialize: function() { return jQuery.para

jQuery源码解析

( function( global, factory ) { "use strict"; if ( typeof module === "object" && typeof module.exports === "object" ) { module.exports = global.document ? factory( global, true ) : function( w ) { if ( !w.document ) {

JQuery中serialize()

一.serialize()定义和用法: serialize()方法通过序列化表单值,创建标准的URL编码文本字符串,它的操作对象是代表表单元素集合的jQuery 对象.你可以选择一个或多个表单元素(比如input或文本框),或者 form 元素本身.序列化的值可在生成 AJAX 请求时用于 URL 查询字符串中. 语法: $(selector).serialize() 详细说明 1..serialize() 方法创建以标准 URL 编码表示的文本字符串.它的操作对象是代表表单元素集合的 jQue