1. jQuery 过滤html标签属性的特殊字符
您好,如果在表单中需要提交一字符串,其中包含,< > " &字符时,当我们把这字符串显示到jsp页面时,会和html标签产生冲突,导致web页面的某些部分消失或者格式不正确。为了解决以上问题,需要在显示之前,对字符串进行代码过滤。
把字符串中的 < 替换为 &It;
> 替换为 >
" 替换为 "
& 替换为 &
这里给出一个静态的过滤代码,供大家参考:
public class StringUtils {
/**
* This method takes a string which may contain HTML tags (ie, <b>,
* <table>, etc) and converts the '<'' and '>' characters to their HTML escape sequences.
* @param input the text to be converted.
* @return the input string with the characters '<' and '>' replaced with their HTML escape sequences.
*/
public static final String escapeHTMLTags(String input) {
//Check if the string is null or zero length -- if so, return
//what was sent in.
if (input == null || input.length() == 0) {
return input;
}
//Use a StringBuffer in lieu of String concatenation -- it is
//much more efficient this way.
StringBuffer buf = new StringBuffer(input.length());
char ch = ' ';
for (int i = 0; i < input.length(); i++) {
ch = input.charAt(i);
if (ch == '<') {
buf.append("<");
}
else if (ch == '>') {
buf.append(">");
}else if(ch == '"'){
buf.append(""");
}else if(ch == '&'){
buf.append("&");
}
else {
buf.append(ch);
}
}
return buf.toString();
}
}
此时,只需在jsp中对字符串调用此方法(StringUtils.escapeHTMLTags(str))即可。
2. jQuery动态过滤table的代码怎么写。
var key=$('input ').val();
$('table tr').each(function(i,item){
$(item).find('td').each(function(t,td){
if($(td).text().indexOf(key)>-1){
$(item).show();
return true;
}
$(item).hide();
});
});
3. jqgrid怎么过滤html标签
^^var var_html;//预移除标签的html
var re = /<[^>]+>/ig;//var re = /<(?!回(\/?img))[^>]+>/ig;//保留答img
var_html= var_html.replace(re, "");
4. 如何用js或则jquery过滤特殊字符
1、jQuery使用正则匹配替换特殊字符
functionRegeMatch(){
varpattern=newRegExp("[~'!@#$%^&*()-+_=:]");
if($("#name").val()!=""&&$("#name").val()!=null){
if(pattern.test($("#name").val())){
alert("非法字符!");
$("#name").attr("value","");
$("#name").focus();
returnfalse;
}
}
}
2、jQuery限制输入ASCII值
//数字0-9的ascii为48-57
//大写A-Z的ascii为65-90
//小写a-z的ascii为97-122
//----------------------------------------------------------------------
//<summary>
//限制只能输入数字和字母
//</summary>
//----------------------------------------------------------------------
$.fn.onlyNumAlpha=function(){
$(this).keypress(function(event){
vareventObj=event||e;
varkeyCode=eventObj.keyCode||eventObj.which;
if((keyCode>=48&&keyCode<=57)||(keyCode>=65&&keyCode<=90)||(keyCode>=97&&keyCode<=122))
returntrue;
else
returnfalse;
}).focus(function(){
this.style.imeMode='disabled';
}).bind("paste",function(){
varclipboard=window.clipboardData.getData("Text");
if(/^(d|[a-zA-Z])+$/.test(clipboard))
returntrue;
else
returnfalse;
});
};
//-----调用方法$("#文本框id").onlyNumAlpha();
3、js正则匹配过滤
functionstripscript(s)
{
varpattern=newRegExp("[`~!@#$^&*()=|{}':;',\[\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]")
varrs="";
for(vari=0;i<s.length;i++){
rs=rs+s.substr(i,1).replace(pattern,'');
}
returnrs;
}
5. 求用jquery或者js 清除指定元素内的 html标签和文本,但是要保留<img />标签
var $imgs= $("#container").find("img");//先从指定抄元素(id=container)内的img找到
$("#container").html("");//清空指定元素内容
$("#container").append($imgs);//把img元素加进去
6. jquery获取html值过滤空格
^var r = "asldfkjl lasdjfl <br /> sdfjlk <br/> ; ; ; ;"
alert(r.replace(/(<br[^>]*>| |\s*)/g,''));
首先应该替换成html能识别的,然后再进专行获属取
7. jQuery JS 属性过滤器多个[attribute=value]如何简写
Firefox下抄jQuery选择器之[attribute^=value]使用注意事项
之前写袭的一个脚本中用到了
[javascript] view plain
var bindAttrs = $("[databind^='attr'", item);
大家都看出存在的问题了吧?
这脚本用了一段时间了,但一直没用Firefox下测试过,
在其它浏览器下都能正常使用!
今天突然有人发现在Firefox下加载的数据不正确,
经过一段时间的调试,最后发现是代码写的有问题,
犯了一个低级的错误,只写了左中括号,忘了写右中括号
8. jquery获取html值过滤空格
|var r = "asldfkjl lasdjfl <br /> sdfjlk <br/> ; ; ; ;"<br>alert(r.replace(/(<br[^>]*>| |\s*)/g,''));<br>首先应该替换成html能识别的,然后版再进行权获取
9. jquery自定义过滤器的正则表达式怎么使用
匹配p标签中的文本满足
apple、orange、lemon三组单词任意一组全部相等返回返回ture。
比如
<p>apple</p>
结果返回ture,
<p>apple1</p>返回false
10. JQuery 多条件过滤
.filter().filter()
筛选之后再筛选就好了