PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > CMS教程 > ecshop教程 > 正文

    解决ecshop和jquery冲突 自定义美化商品属性选择

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:jquery用在ecshop中使用时会和ecshop本身对ajax和json的解析发生冲突,这是因为两者都重写了Object,现在我们来解决ecshop和jquery冲突的问题...
    jquery用在ecshop中使用时会和ecshop本身对ajax和json的解析发生冲突,这是因为两者都重写了Object,现在我们来解决ecshop和jquery冲突的问题,然后对ecshop端口属性选择进行自定义样式美化.

    方法如下:

    拷贝一个transport.js 为 transport1.js,在需要用到的页面插入这个新js,隐藏 586行处开始:

    1. /*
    2. Object.prototype.toJSONString = function () {
    3. var a = ['{'], // The array holding the text fragments.
    4. b, // A boolean indicating that a comma is required.
    5. k, // The current key.
    6. v; // The current value.
    7.  
    8. function p(s) {
    9.  
    10. // p accumulates text fragment pairs in an array. It inserts a comma before all
    11. // except the first fragment pair.
    12.  
    13. if (b) {
    14. a.push(',');
    15. }
    16. a.push(k.toJSONString(), ':', s);
    17. b = true;
    18. }
    19.  
    20. // Iterate through all of the keys in the object, ignoring the proto chain.
    21.  
    22. for (k in this) {
    23. if (this.hasOwnProperty(k)) {
    24. v = this[k];
    25. switch (typeof v) {
    26.  
    27. // Values without a JSON representation are ignored.
    28.  
    29. case 'undefined':
    30. case 'function':
    31. case 'unknown':
    32. break;
    33.  
    34. // Serialize a JavaScript object value. Ignore objects that lack the
    35. // toJSONString method. Due to a specification error in ECMAScript,
    36. // typeof null is 'object', so watch out for that case.
    37.  
    38. case 'object':
    39. if (this !== window)
    40. {
    41. if (v) {
    42. if (typeof v.toJSONString === 'function') {
    43. p(v.toJSONString());
    44. }
    45. } else {
    46. p("null");
    47. }
    48. }
    49. break;
    50. default:
    51. p(v.toJSONString());
    52. }
    53. }
    54. }
    55.  
    56. // Join all of the fragments together and return.
    57.  
    58. a.push('}');
    59. return a.join('');
    60. };
    61. */
    62.  
    63. 修改 common.js getSelectedAttributes方法。(如果需要的话)这里是解决商品商品属性点击的时候切换价格的
    64. /**
    65. * 获得选定的商品属性
    66. */
    67. function getSelectedAttributes(formBuy)
    68. {
    69. var spec_arr = new Array();
    70. var j = 0;
    71.  
    72. for (i = 0; i < formBuy.elements.length; i ++ )
    73. {
    74. var prefix = formBuy.elements[i].name.substr(0, 5);
    75.  
    76. if (prefix == 'spec_' && (
    77. ((formBuy.elements[i].type == 'hidden' || formBuy.elements[i].type == 'checkbox') && formBuy.elements[i].checked) ||
    78. formBuy.elements[i].tagName == 'SELECT'))
    79. {
    80. spec_arr[j] = formBuy.elements[i].value;
    81. j++ ; //www.bcty365.com
    82. }
    83. }
    84.  
    85. return spec_arr;
    86. }

    common.js 加入:

    1. </pre>
    2. function obj2str(o){
    3. var r = [];
    4. if(typeof o =="string") return "\""+o.replace(/([\'\"\\])/g,"\\$1").replace(/(\n)/g,"\\n").replace(/(\r)/g,"\\r").replace(/(\t)/g,"\\t")+"\"";
    5. if(typeof o =="undefined") return "undefined";
    6. if(typeof o == "object"){
    7. if(o===null) return "null";
    8. else if(!o.sort){
    9. for(var i in o)
    10. r.push("\""+i+"\""+":"+obj2str(o[i]))
    11. r="{"+r.join()+"}"
    12. }else{
    13. for(var i =0;i<o.length;i++)
    14. r.push(obj2str(o[i]))
    15. r="["+r.join()+"]"
    16. }
    17. return r;
    18. }
    19. return o.toString();
    20. }

    加入购物车改为:

    Ajax.call('flow.php?step=add_to_cart', 'goods=' + obj2str(goods), addToCartResponse,'POST', 'JSON');

    一招解决,解决ECSHOP中transport.js和jquery的冲突.

    在page_header.lbi文件的最后面添加下面的代码即可,经IETester测试后,ie6及以上都可行.

    1. {insert_scripts files='niuzai/jquery-1.8.3.js'}
    2. <script type="text/javascript">
    3. $(function() {
    4. window.__Object_toJS**tring = Object.prototype.toJS**tring;
    5. delete Object.prototype.toJS**tring;
    6. });
    7. </script>
    8. {insert_scripts files='test.js'}

    注:**为大写O-N-S,去掉中间的两个-,被屏蔽了,无语,先声明一下,这不是我自创的,而是看了论坛里的兄弟的相关帖子后,然后我试了没用,因为导入文件顺序的问题,导致不能解决,所以就发了这个帖子,和大家分享分享。test.js为自己用jquery写的一些代码,要放在jquery文件的后面,注意不能和jquery文件一同导入,注意顺序,否则会出错,顺序为:先导入transport.js文件{insert_scripts files='transport.js,utils.js'},然后导入jquery文件,{insert_scripts files='niuzai/jquery-1.8.3.js'} 再加上这段代码:

    1. <script type="text/javascript">
    2. $(function() {
    3. window.__Object_toJS**tring = Object.prototype.toJS**tring;
    4. delete Object.prototype.toJS**tring;
    5. });
    6. </script>

    最后引入自己用jquery书写的js文件.{insert_scripts files='test.js'}

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-104-2623-1.html
    相关热词搜索: