87,919
社区成员
发帖
与我相关
我的任务
分享
/* 格式化获取到的HTML */
/*
(1)BR替换成P,然后DIV也替换成P
(2)并且连接都在target="_blank"
(3)返回替换好之后的内容,传入的也是string,返回的也是string
*/
function FormatHtml(str){
if(str.length == 0) return '';
var HTML = $(str);
/* 去除所有样式 */
HTML.find('*').removeAttr('style').removeAttr('class');
HTML.find('div').replaceWith(function(){
return $(this).contents();
});
/* 过滤回车BR */
HTML.find('br').replaceWith(function(){
return '<br />';
});
/* 过滤样式 */
HTML.find('p').replaceWith(function(){
return '<p>'+$(this).contents()+'</p>';
});
/* 连接过滤[不要图片之类的] */
HTML.find('a').replaceWith(function(){
if($(this).attr('href').substring(0,4) == 'http'){
return '<a href="'+$(this).attr('href')+'" target="_blank">'+$(this).text()+'</a>';
}else{
return '';
}
});
/* 这里是只返回“职位职能: ”这四个字 */
return HTML.html();
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
</head>
<body>
<div style="font-size:15px;" class="divbg job_detail">
<strong>职位职能:</strong> 手机软件开发工程<div class="jobs_com">
<div class="graybttop">
<span><img src="http://img01.51jobcdn.com/im/2009/graybttl.gif" align="left"/></span>
<span><img src="http://img01.51jobcdn.com/im/2009/graybttr.gif" align="right"/></span>
<div class="btname" style="overflow:auto;"><a name="gsjj"></a><table border="0" width="100%" cellspacing="0" cellpadding="0" style="background-image: url(http://img01.51jobcdn.com/im/2009/search/c/gsjjbt.gif);background-repeat: no-repeat;"><tr><td width="65%"><input name="coid" type="hidden" value="2547579"/></td><td width="35%" align="right" id="2547579"></td></tr></table></div>
</div>
<div class="grayline">
<p class="graybttopbg"></p>
<div class="jobs_txt">
<p class="txt_font">XXX公司总部位于德国Eiterfeld,多年来专业从事高精密全自动视觉贴片机和工业控制机器人以及高速高精密运动控制模组的技术开发和生产。<br>XXX中国公司依托德国的技术优势和品质管理优势,主要服务于中国市场的客户,致力于提供最高性价比的产品和优质的服务。公司坐落于北京市通州区金桥科技产业基地联东U谷工业园中区15栋,产品和服务主要有:全自动视觉贴片系统,精密专用数控机床系统,自动化控制软件和伺服系统;还有专门的以客户为导向的定制服务。<br>XXX在电子生产企业升级换代中,真正解决了目前用工成本高、生产效率低、产品质量不稳定、设备投入高、维护成本高等电子企业和电子厂经常遇到的问题,同时提供最适合中国国内中小型企业的解决方案,已经为多家研究机构、国家重点实验室,科研院所和大型外企的新产品试制中心采用。尤其适合LED灯条、LED显示屏生产企业、汽车电子、智能通讯等企业的批量生产。欢迎广大用户光临指导!</p>
<p class="pot2"></p><p class="txt_font1">公司网站:<a href="http://www.zbphp.com" target="_blank">http://www.zbphp.com</a></p><p class="txt_font1">联 系 人:HR招聘专员</p><p class="txt_font1">地 址:北京市通州区马驹桥联东X谷科技园XXX号<br> <a href="javascript:void(0);" class="mapurlfont" style="color:#256db9;" onclick="zzSearch.encodeUrl('北京','北京市通州区马号','116.559800,39.731200','1');return false;"><img src="http://img01.51jobcdn.com/im/2009/search/1.gif" align="absmiddle" />具体位置</a> <a href="javascript:void(0);" class="mapurlfont" style="color:#256db9;" onclick="zzSearch.encodeUrl('北京','北京市通州区马驹桥联东U号','116.559800,39.731200','2','2547579');return false;"><img src="http://img01.51jobcdn.com/im/2009/search/2.gif" align="absmiddle" />公交地铁</a> <a href="javascript:void(0);" class="mapurlfont" style="color:#256db9;" onclick="zzSearch.encodeUrl('北京','北京市通州区马驹桥联东XX谷科技园XXXX号','116.559800,39.731200','3');return false;"><img src="http://img01.51jobcdn.com/im/2009/search/3.gif" align="absmiddle" />周边环境</a> <a href="http://search.51job.com/list/0100,%2B,%2B,%2B,%2B,%2B,%2B,%2B,1.html?lang=c&stype=3&address=%B1%B1%BE%A9%CA%D0%CD%A8%D6%DD%C7%F8%C2%ED%BE%D4%C7%C5%C1%AA%B6%ABU%B9%C8%BF%C6%BC%BC%D4%B015%BA%C5&radius=0.03&fromType=17" class="mapurlfont" style="color:#256db9;"><img src="http://img01.51jobcdn.com/im/2009/search/4.gif" align="absmiddle" />周边职位</a> <a href="javascript:void(0);" class="mapurlfont" style="color:#256db9;" onclick="smsEntry('http://my.51job.com','2547579'); return false;"><img src="http://img01.51jobcdn.com/im/2009/search/ico-phone.gif" align="absmiddle" style="margin-right:5px" />发送到手机</a></p><p class="txt_font1">邮政编码:101102</p><p class="txt_font1">在线联系:旺旺(淘宝版)ID:<span class="zz_51color" title="把对方加为旺旺好友后才可联系">sXXXXXm</span></p>
</div>
</div>
<div class="graybott">
<span><img src="http://img01.51jobcdn.com/im/2009/graybl.gif" align="left"/></span>
<span><img src="http://img01.51jobcdn.com/im/2009/graybr.gif" align="right"/></span>
</div>
</div>
</div>
<script language="javascript" type="text/javascript">
<!--
/* 格式化获取到的HTML */
/*
(1)BR替换成P,然后DIV也替换成P
(2)并且连接都在target="_blank"
(3)返回替换好之后的内容,传入的也是string,返回的也是string
*/
function FormatHtml(str){
if(str.length == 0) return '';
var HTML = $(str);
/* 去除所有样式 */
HTML.find('*').removeAttr('style').removeAttr('class');
HTML.find('div').replaceWith(function(){
return $(this).contents();
});
/* 过滤回车BR */
HTML.find('br').replaceWith(function(){
return '<br />';
});
/* 过滤样式 */
HTML.find('p').replaceWith(function(){
return '<p>'+$(this).contents()+'</p>';
});
/* 连接过滤[不要图片之类的] */
HTML.find('a').replaceWith(function(){
if($(this).attr('href').substring(0,4) == 'http'){
return '<a href="'+$(this).attr('href')+'" target="_blank">'+$(this).text()+'</a>';
}else{
return '';
}
});
/* 这里是只返回“职位职能: ”这四个字 */
return HTML.html();
}
var HTML = $('.divbg');
var s = FormatHtml( HTML.html() );
alert( s );
//-->
</script>
</body>
</html>
/* 传入OBJ */
function FormatHtml(HTML){
if(HTML.length == 0) return '';
/* 去除所有样式 */
HTML.find('*').removeAttr('style')
.removeAttr('class');
HTML.find('img').remove();
HTML.find('div').replaceWith(function(i,str){
$(this).html( $.trim( $(this).html() ) );
if($(this).html().substring(0,3) == '<p>'){
return $(this).html();
}else{
return '<p>' + $(this).html() + '</p>';
}
});
/* 过滤回车BR */
HTML.find('br').replaceWith(function(){
return '<br />';
});
/* 过滤样式 */
HTML.find('p').replaceWith(function(){
return '<p>'+ $.trim( $(this).html() )+'</p>';
});
/* 连接过滤[不要图片之类的] */
HTML.find('a').replaceWith(function(){
// 51job.com 字符串长度为 9
if($(this).attr('href').substring(0,4) == 'http' && $(this).attr('host').substr(-9,9).toLowerCase() != '51job.com'){
return '<a href="'+$(this).attr('href')+'" target="_blank">'+ $.trim( $(this).text() )+'</a>';
}else{
return '';
}
});
return 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
id="notMe"
P
jQuery 代码: $("*") 结果: [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. 返回值 Arrayp class="myClass"
spanp class="notMyClass"
jQuery 代码: $("div,span,p.myClass") 结果: [p class="myClass"
, span ] 层级 ancestor descendantancestor descendant 在给定的祖先元素下匹配所有的后代元素 -------------------------------------------------------------------------------- Matches all descendant elements specified by descendant of elements specified by ancestor. 返回值 ArrayHeader 1 |
Value 1 |
Value 2 |
Header 1 |
Value 1 |
Value 2 |
Header 1 |
Value 1 |
Value 2 |
Header 1 |
Value 1 |
Value 2 |
Header 1 |
Value 1 |
Value 2 |
Header 1 |
Value 1 |
Value 2 |
Header 1 |
Value 1 |
Value 2 |
Contents 1
Contents 2
jQuery 代码: $(":header").css("background", "#EEE"); 结果: [Value 1 | |
Value 2 |
Hello
Hello
Value 1 | |
Value 2 |
Value 2 |
Value 2 |
Hello!
Value 2 |
Hello
jQuery 代码: $("p").addClass("selected"); 结果: [Hello
] -------------------------------------------------------------------------------- 为匹配的元素加上 selected highlight 类 HTML 代码:Hello
jQuery 代码: $("p").addClass("selected highlight"); 结果: [Hello
] removeClass(class)removeClass(class) 从所有匹配的元素中删除全部或者指定的类。 -------------------------------------------------------------------------------- Removes all or the specified class(es) from the set of matched elements. 返回值 jQuery 参数 class (String) : (可选) 一个或多个要删除的CSS类名,请用空格分开 示例 从匹配的元素中删除 'selected' 类 HTML 代码:Hello
jQuery 代码: $("p").removeClass("selected"); 结果: [Hello
] -------------------------------------------------------------------------------- 删除匹配元素的所有类 HTML 代码:Hello
jQuery 代码: $("p").removeClass(); 结果: [Hello
] toggleClass(class)toggleClass(class) 如果存在(不存在)就删除(添加)一个类。 -------------------------------------------------------------------------------- Adds the specified class if it is not present, removes the specified class if it is present. 返回值 jQuery 参数 class (String) :CSS类名 示例 为匹配的元素切换 'selected' 类 HTML 代码:Hello
Hello Again
jQuery 代码: $("p").toggleClass("selected"); 结果: [Hello
,Hello Again
] Html代码 html()html() 取得第一个匹配元素的html内容。这个函数不能用于XML文档。但可以用于XHTML文档。 -------------------------------------------------------------------------------- Get the html contents of the first matched element. This property is not available on XML documents (although it will work for XHTML documents). 返回值 String 示例 HTML 代码:Hello
Hello Again
"); 结果: [Hello Again
Test Paragraph.
Paraparagraph
jQuery 代码: $("p").text(); 结果: Test Paragraph.Paraparagraph text(val)text(val) 设置所有匹配元素的文本内容 与 html() 类似, 但将编码 HTML (将 "<" 和 ">" 替换成相应的HTML实体). -------------------------------------------------------------------------------- Set the text contents of all matched elements. Similar to html(), but escapes HTML (replace "<" and ">" with their HTML entities). 返回值 jQuery 参数 val (String) : 用于设置元素内容的文本 示例 HTML 代码:Test Paragraph.
jQuery 代码: $("p").text("Some new text."); 结果: [Some new text.
] 值 val()val() 获得第一个匹配元素的当前值。 在 jQuery 1.2 中,可以返回任意元素的值了。包括select。如果多选,将返回一个数组,其包含所选的值。 -------------------------------------------------------------------------------- Get the content of the value attribute of the first matched element. In jQuery 1.2, a value is now returned for all elements, including selects. For multiple selects an array of values is returned. 返回值 String,Array 示例 获得单个select的值和多选select的值。 HTML 代码:Single:SingleMultiple:Multiple, Multiple3
] -------------------------------------------------------------------------------- 获取文本框中的值 HTML 代码: jQuery 代码: $("input").val(); 结果: some text val(val)val(val) 设置每一个匹配元素的值。 在 jQuery 1.2, 这也可以为select元件赋值 -------------------------------------------------------------------------------- Set the value attribute of every matched element. In jQuery 1.2, this is also able to set the value of select elements, but selecting the appropriate options. 返回值 jQuery 参数 val (String) : 要设置的值。 示例 设定文本框的值 HTML 代码: jQuery 代码: $("input").val("hello world!"); val(val)val(val) check,select,radio等都能使用为之赋值 返回值 jQuery 参数 val (ArrayThis is just a test.
So is this
jQuery 代码: $("p").eq(1) 结果: [So is this
] hasClass(class)hasClass(class) 检查当前的元素是否含有某个特定的类,如果有,则返回true。 这其实就是 is("." + class)。 -------------------------------------------------------------------------------- Checks the current selection against a class and returns true, if at least one element of the selection has the given class. This is an alternative to is("." + class). 返回值 Boolean 参数 class (String) : 用于匹配的类名 示例 给包含有某个类的元素进行一个动画。 HTML 代码: jQuery 代码: $("div").click(function(){ if ( $(this).hasClass("protected") ) $(this) .animate({ left: -10 }) .animate({ left: 10 }) .animate({ left: -10 }) .animate({ left: 10 }) .animate({ left: 0 }); }); filter(expr)filter(expr) 筛选出与指定表达式匹配的元素集合。 这个方法用于缩小匹配的范围。用逗号分隔多个表达式 -------------------------------------------------------------------------------- Removes all elements from the set of matched elements that do not match the specified expression(s). This method is used to narrow down the results of a search. Provide a comma-separated list of expressions to apply multiple filters at once. 返回值 jQuery 参数 expr (Expression) : 表达式 示例 保留带有select类的元素 HTML 代码:Hello
Hello Again
And Again
jQuery 代码: $("p").filter(".selected") 结果: [And Again
] -------------------------------------------------------------------------------- 保留第一个以及带有select类的元素 HTML 代码:Hello
Hello Again
And Again
jQuery 代码: $("p").filter(".selected, :first") 结果: [Hello
,And Again
] filter(fn)filter(fn) 筛选出与指定函数返回值匹配的元素集合 这个函数内部将对每个对象计算一次 (正如 '$.each'). 如果调用的函数返回false则这个元素被删除,否则就会保留。 -------------------------------------------------------------------------------- Removes all elements from the set of matched elements that does not match the specified function. The function is called with a context equal to the current element (just like '$.each'). If the function returns false, then the element is removed - anything else and the element is kept. 返回值 jQuery 参数 fn (Function) : 传递进filter的函数 示例 保留子元素中不含有ol的元素。 HTML 代码:How are you?
jQuery 代码: $("p").filter(function(index) { return $("ol", this).length == 0; }); 结果: [How are you?
] is(expr)is(expr) 用一个表达式来检查当前选择的元素集合,如果其中至少有一个元素符合这个给定的表达式就返回true。 如果没有元素符合,或者表达式无效,都返回'false'. 'filter' 内部实际也是在调用这个函数,所以,filter()函数原有的规则在这里也适用。 -------------------------------------------------------------------------------- Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression. If no element fits, or the expression is not valid, then the response will be 'false'. 'filter' is used internally, therefore all rules that apply there apply here, as well. 返回值 Boolean 参数 expr (String) :用于筛选的表达式 示例 由于input元素的父元素是一个表单元素,所以返回true。 HTML 代码: jQuery 代码: $("input[type='checkbox']").parent().is("form") 结果: true map(callback)map(callback) 将一组元素转换成其他数组(不论是否是元素数组) 你可以用这个函数来建立一个列表,不论是值、属性还是CSS样式,或者其他特别形式。这都可以用'$.map()'来方便的建立。 -------------------------------------------------------------------------------- Translate a set of elements into another set of values (which may, or may not, be elements). You could use this to build lists of values, attributes, css values - or even perform special, custom, selector transformations. This is provided as a convenience method for using '$.map()'. 返回值 jQuery 参数 callback (Function) : 给每个元素执行的函数 示例 把form中的每