当html中存在url中如: onclick="toView('参数1')", 参数1是特别字符,如&asop;"' "等时,浏览器解析时会报错。解决方法如文中描述...

解决方案: 自定义标签将字符串转换成unicode编码后输出显示到页面即可

解析原理:解析顺序html  ---url ----javascript---url,由于unicode编码在htm解析阶段
不被解析保留原来文本格式,在javascript触发时才解析。
因此要让页面不报错,文本保留原格式,采用unicode转换即可。


以单引号为例,单引号表达方式有以下几种
'    "    \u0027    '
查看document树发现只有\u0027保持原来的文本格式。浏览器便不会报错,这是我们想要的。
而且在js调用时,会将unicode转换回原本格式,提交到后台。
原内容    unicode编码格式
'             \u0027
"    \u0026\u0071\u0075\u006f\u0074\u003b

\u0027   \u005c\u0075\u0030\u0030\u0032\u0037

'     \u0026\u0023\u0033\u0039\u003b


流程:
自定义标签将原格式转换成unicode显示到页面
js将unicode转换成原格式提交到后台
后台用原格式参数去查询 
 
unicode编码简而言之就是将每一个字符用16位2进制数标识。但是通常都用4位的16进制数标识。 
例如: 
1)中文字符串"你好"的unicode码为:\u60\u597d; 
2)英文字符串"ab"的unicode码为:\u0061\u0062; 
其中\u是标识unicode码用的,后面的4位16进制数则是对应字符的unicode码。 

unicode码在J2EE项目中应用广泛,java对unicode码提供了很好的支持。例如国际化,则是unicode的经典运用。 
那么unicode的编码规则具体是什么,如何用程序实现? 

1、unicode编码规则 

   unicode码对每一个字符用4位16进制数表示。具体规则是:将一个字符(char)的高8位与低8位分别取出,转化为16进制数, 
   如果转化的16进制数的长度不足2位,则在其后补0,然后将高、低8位转成的16进制字符串拼接起来并在前面补上"\u" 即可。 
   
2、转码程序 

1)字符串转unicode 

/** 
* 将字符串转成unicode 
* @param str 待转字符串 
* @return unicode字符串 
*/ 
public String convert(String str) 

str = (str == null ? "" : str); 
String tmp; 
StringBuffer sb = new StringBuffer(1000); 
char c; 
int i, j; 
sb.setLength(0); 
for (i = 0; i < str.length(); i++) 

c = str.charAt(i); 
sb.append("\\u"); 
j = (c >>>8); //取出高8位 
tmp = Integer.toHexString(j); 
if (tmp.length() == 1) 
sb.append("0"); 
sb.append(tmp); 
j = (c & 0xFF); //取出低8位 
tmp = Integer.toHexString(j); 
if (tmp.length() == 1) 
sb.append("0"); 
sb.append(tmp); 


return (new String(sb)); 



2)unicode转成字符串,与上述过程反向操作即可 
/** 
* 将unicode 字符串 
* @param str 待转字符串 
* @return 普通字符串 
*/ 
public String revert(String str) 

str = (str == null ? "" : str); 
if (str.indexOf("\\u") == -1)//如果不是unicode码则原样返回 
return str; 

StringBuffer sb = new StringBuffer(1000); 

for (int i = 0; i < str.length() - 6;) 

String strTemp = str.substring(i, i + 6); 
String value = strTemp.substring(2); 
int c = 0; 
for (int j = 0; j < value.length(); j++) 

char tempChar = value.charAt(j); 
int t = 0; 
switch (tempChar) 

case 'a': 
t = 10; 
break; 
case 'b': 
t = 11; 
break; 
case 'c': 
t = 12; 
break; 
case 'd': 
t = 13; 
break; 
case 'e': 
t = 14; 
break; 
case 'f': 
t = 15; 
break; 
default: 
t = tempChar - 48; 
break; 


c += t * ((int) Math.pow(16, (value.length() - j - 1))); 

sb.append((char) c); 
i = i + 6; 

return sb.toString(); 
 

转载于:https://www.cnblogs.com/huzefeng/p/5042946.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<!--index.wxml--> <scroll-view class=&quot;container&quot; scroll-y=&quot;true&quot; scroll-into-view=&quot;{{toView}}&quot;  scroll-top=&quot;{{scrollTop}}&quot; bindscroll=&quot;scroll&quot;> <swiper indicator-dots=&quot;{{indicatorDots}}&quot;   autoplay=&quot;{{autoplay}}&quot; interval=&quot;{{interval}}&quot; duration=&quot;{{duration}}&quot; class=&quot;swiper-box&quot; id=&quot;top&quot;>   <block wx:for-items=&quot;{{banner_list[0].banner}}&quot; wx:key=&quot;index&quot;>     <swiper-item >       <image class=&quot;slide-image&quot; src=&quot;{{item.pic_url}}&quot;/>     </swiper-item>   </block> </swiper>  <view>   <view class='menueview' >    <block wx:for-items=&quot;{{banner_list[1].banner}}&quot; wx:key=&quot;index&quot;>      <view  class=&quot;menue-1&quot;>         <view class=&quot;menueimg&quot;>             <image src=&quot;{{item.pic_url}}&quot; class=&quot;menue-image&quot; />         </view>         <view class=&quot;menuetext&quot;>{{item.title}}</view>     </view>    </block>   </view>  </view> <view class='hg-container' id=&quot;new&quot; >   <text class=&quot;hg-title&quot;>新品推荐</text>         <view class='gooditem'>             <view class='imgview' wx:key=&quot;index&quot;  wx:for=&quot;{{newgoods}}&quot; wx:for-index=&quot;idx&quot; wx:for-item=&quot;good&quot;>               <image class='productimg' src='/images/icons/1.webp'/>             </view>         </view> </view> <view class='hg-container'>   <text class=&quot;hg-title&quot;>超值买</text>         <view class='gooditem'>             <view class='imgview' wx:key=&quot;index&quot;  wx:for=&quot;{{newgoods}}&quot; wx:for-index=&quot;idx&quot; wx:for-item=&quot;good&quot;>               <image class='productimg' src='/images/icons/2.webp'/>             </view>         </view> </view> <view class='hot-container'>   <view class=&quot;hot-title&quot;>食惠购</view>         <view class='hot-gooditem'>             <view class='hot-imgview' wx:key=&quot;index&quot;  wx:for=&quot;{{hotgoods}}&quot; wx:for-index=&quot;idx&quot; wx:for-item=&quot;hot&quot;>               <image class='hot-product' src='{{hot.more_pic}}'/>             </view>         </view> </view> <view class=&quot;footer&quot; bindtap=&quot;tap&quot;>   <view>   <text>沪ICP备888888888号</text>   <text>htmlk.cn</text>   <text>微信商城有限公司</text>   <text>北京市xxxxxxxxx</text>   </view> </view> </scroll-view>
最新发布
06-08

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值