解决easyui tabs中href无法跨域跳转

前端之家收集整理的这篇文章主要介绍了解决easyui tabs中href无法跨域跳转前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <Meta http-equiv="content-type" content="text/html" />
  5. <Meta name="author" content="blog.anchen8.net" />
  6. <script type="text/javascript" src="__PUBLIC__/jquery-easyui-1.4.1/jquery.min.js"></script>
  7. <script type="text/javascript" src="__PUBLIC__/jquery-easyui-1.4.1/jquery.easyui.min.js"></script>
  8. <link rel="stylesheet" type="text/css" href="__PUBLIC__/jquery-easyui-1.4.1/themes/default/easyui.css">
  9. <link rel="stylesheet" type="text/css" href="__PUBLIC__/jquery-easyui-1.4.1/themes/icon.css">
  10. <title>easyui测试</title>
  11. <style>
  12. .ico{width:300px;height:200px;float:left;}
  13. .menu{width:150px;height:50px;margin-top:5px;}
  14. ul{text-align:left;}
  15. li{text-align:left;}
  16. </style>
  17. <script>
  18. var url="<{:U('User/getUserList')}>";
  19. $(function(){
  20. //tabs绑定右键事件
  21. $('#tabs').tabs({
  22. //添加默认tab
  23.  
  24. onContextMenu:function(e,title){
  25. e.preventDefault();//取消对象e的默认事件
  26. $('#rightmenu').menu('show',{
  27. left: e.pageX,top: e.pageY
  28. });
  29. $('#tabs').tabs('select',title);
  30. },});
  31. //tree绑定单击事件
  32. $('#menu_tree').tree({
  33. onClick:function(node){
  34. //添加节点到panel中
  35. //alert(JSON.stringify(node));
  36. var title=node.text;
  37. if($('#tabs').tabs('exists',title) || node.children){
  38. $('#tabs').tabs('select',title);
  39. }else{
  40. $('#tabs').tabs('add',{
  41. title:title,closable:true,//content: '<iframe src="' + url + '" frameborder=0 height=100% width=100% scrolling=no></iframe>',//frame加载所需要页面。href:只是加载页面,且不能跨域
  42. <strong> </strong><span style="color:#000099;"><strong>href:'www.baidu.com'</strong>,</span>
  43. msg :"正在加载数据,请稍候...",tabWidth:100
  44. });
  45. }
  46. }
  47. });
  48. // 全部关闭
  49. $('#closeall').click(function() {
  50. var taball = $('#tabs').tabs('tabs');
  51. var title = [];
  52. var j = 0;
  53. for (var i = 0 in taball) {
  54. <span style="color:#CC0000;">console.dir(taball[i].panel('options'));</span>
  55. if (taball[i].panel('options').closable) {
  56. title[j] = taball[i].panel('options').title;
  57. j++;
  58. title.length = j;
  59. }
  60. }
  61. for (var v = 0 in title) {
  62. $('#tabs').tabs('close',title[v]);
  63. }
  64. });
  65. // 关闭当前
  66. $('#closethis').click(function() {
  67. var currTab = $('#tabs').tabs('getSelected');
  68. if (currTab.panel('options').closable) {
  69. $('#tabs').tabs('close',currTab.panel('options').title);
  70. }
  71. });
  72. // 关闭除当前之外的TAB
  73. $('#cloSEOther').click(function() {
  74. var taball = $('#tabs').tabs('tabs');
  75. var currTab = $('#tabs').tabs('getSelected');
  76. var currTitle = currTab.panel('options').title;
  77. var title = [];
  78. var j = 0;
  79. for (var i = 0 in taball) {
  80. if (taball[i].panel('options').title != currTitle) {
  81. if (taball[i].panel('options').closable) {
  82. title[j] = taball[i].panel('options').title;
  83. j++;
  84. title.length = j;
  85. }
  86. }
  87. }
  88. if (j == 0) {
  89. showMsg('没有可关闭的选项卡了!');
  90. } else {
  91. for (var v = 0 in title) {
  92. $('#tabs').tabs('close',title[v]);
  93. }
  94. }
  95. return false;
  96. });
  97. // 关闭当前右侧的TAB
  98. $('#closeright').click(function() {
  99. var taball = $('#tabs').tabs('tabs');
  100. var currTab = $('#tabs').tabs('getSelected');
  101. var currTitle = currTab.panel('options').title;
  102. var title = [];
  103. var j = 0;
  104. var st = false;
  105. for (var i = 0 in taball) {
  106. if (currTitle == taball[i].panel('options').title) {
  107. st = true;
  108. }
  109. if (st) {
  110. if (taball[i].panel('options').title != currTitle) {
  111. if (taball[i].panel('options').closable) {
  112. title[j] = taball[i].panel('options').title;
  113. j++;
  114. title.length = j;
  115. }
  116. }
  117. }
  118. }
  119. if (j == 0) {
  120. showMsg('右侧没有可关闭的选项卡了!');
  121. } else {
  122. for (var v = 0 in title) {
  123. $('#tabs').tabs('close',title[v]);
  124. }
  125. }
  126. return false;
  127. });
  128. // 关闭当前左侧的TAB
  129. $('#closeleft').click(function() {
  130. var taball = $('#tabs').tabs('tabs');
  131. var currTab = $('#tabs').tabs('getSelected');
  132. var currTitle = currTab.panel('options').title;
  133. var title = [];
  134. var j = 0;
  135. var st = true;
  136. for (var i = 0 in taball) {
  137. if (currTitle == taball[i].panel('options').title) {
  138. st = false;
  139. }
  140. if (st) {
  141. if (taball[i].panel('options').closable) {
  142. title[j] = taball[i].panel('options').title;
  143. j++;
  144. title.length = j;
  145. }
  146. }
  147. }
  148. if (j == 0) {
  149. showMsg('左侧侧没有可关闭的选项卡了!');
  150. } else {
  151. for (var v = 0 in title) {
  152. $('#tabs').tabs('close',title[v]);
  153. }
  154. }
  155. });
  156. // 退出
  157. $("#mmexit").click(function() {
  158. $('#tabmenus').menu('hide');
  159. });
  160. });
  161. </script>
  162. </head>
  163.  
  164. <body class="easyui-layout" id="body">
  165. <div data-options="region:'north',split:true" style="height:100px;">
  166. <div class="ico">按钮一</div>
  167. <div class="ico">按钮二</div>
  168. <div class="ico">按钮三</div>
  169. <div class="ico">按钮四</div>
  170. </div>
  171. <div data-options="region:'west',split:true" style="width:150px;" >
  172. <!--
  173. <div style="width:150px;height:50px;margin-top:5px;border:1px solid gray">系统设置</div>
  174. <div style="width:150px;height:50px;margin-top:5px;border:1px solid gray">会员管理</div>
  175. <div style="width:150px;height:50px;margin-top:5px;border:1px solid gray">每日上市</div>
  176. -->
  177. <div class="easyui-accordion">
  178. <div title="导航菜单" data-options="iconCls:'icon-nav',collapsible:false" style="overflow:auto;padding:10px;">
  179. </div>
  180. <div title="便利店设置" data-options="iconCls:'icon-set',selected:true" style="padding:10px;">
  181. <ul class="easyui-tree" id="menu_tree">
  182. <li>大厨特制</li>
  183. <li>小吃东西</li>
  184. <li>
  185. <span>其他东西</span>
  186. <ul>
  187. <li>方便面</li>
  188. <li>挂面</li>
  189. <li></li>
  190. </ul>
  191. </li>
  192. </ul>
  193. </div>
  194. <div title="便利店设置" style="padding:10px;">
  195. </div>
  196. </div>
  197. </div>
  198. <div data-options="region:'center',split:true" id="tab_parent">
  199. <div class="easyui-tabs" id="tabs" style="height:800px;">
  200. <div title="系统设置" data-options="closable:false" style="overflow:auto;padding:20px;display:none;">
  201. 系统设置
  202. </div>
  203. </div>
  204. </div>
  205.  
  206.  
  207. <div id="rightmenu" class="easyui-menu" style="">
  208. <div data-options="iconCls:'icon-cancel'" id="closethis">
  209. 关闭
  210. </div>
  211. <div id="closeall">
  212. 关闭全部
  213. </div>
  214. <div id="cloSEOther">
  215. 关闭其他
  216. </div>
  217. <div class="menu-sep"></div>
  218. <div id="closeright">
  219. 关闭右侧标签
  220. </div>
  221. <div id="closeleft">
  222. 关闭左侧标签
  223. </div>
  224. </div>
  225. </body>
  226. </html>

tabs加载页面分为两种方式

href方式加载数据的特点:

  1. 被加载的页面只有body元素内部的内容才会被加载,也就是jQuery的ajax请求的只是html片段。
  2. 加载远程url时有遮罩效果,也就是“等待中……”效果用户体验较好。
  3. 当加载的页面布局较为复杂,或者有较多的js脚本需要运行的时候,编码往往就需要谨慎了,容易出问题,后面会详细谈
    content方式加载数据的特点:
  1. 比较灵活,你可以在脚本里面拼写HTML代码,然后赋值给tab的content属性,不过这种写法会使得代码易读性变差。
  2. 可以把iframe赋给content,把一个iframe嵌入也就没有什么不能完成的了。
  3. 使用iframe会造成客户端js重复加载,浪费资源,比如说你主页面要引用easyui的库,你的iframe也要引用,浪费就产生了。

代码蓝色字体(href:'www.baidu.com',)中,easyui无法加载百度网站的内容,换其他网站尝试也不可以。究其原因在于,浏览器在进行ajex跳转时,为了保证服务器安全和数据安全,浏览器会禁止跨域ajex的请求。打个比方:某个网站的某张页面被你写入了一些js,这些js有些ajax操作
如果某个用户访问了这张页面,你的js就可以获得用户的某些信息(cookie,本地文件等)然后通过ajax发送回你的服务器。 这样你就可以获得任意你想获得的数据了。ajex的存在本身就是简化请求和不断刷新,禁用跨域可以保证ajex使用者的安全。因此easyui在href中,也会禁止ajex的跨域请求。但是并不是说你想加载其他域名的内容不能实现。可以采用content加载方式。将蓝色文字替换为:

content: '<iframe src="' + url + '" frameborder=0 height=100% width=100% scrolling=no></iframe>' 其中url替换成任意的网址即可。

问题:

1.tabs是panel的子类,tabs依赖于panel,那为什么64行部分不能改为:希望大家指教下

  1. console.dir(taball[i].tabs('options'));

猜你在找的Ajax相关文章