利用正則表示式來解決ueditor上傳的圖片太大(absolutely available)
阿新 • • 發佈:2019-01-30
在用ueditor上傳圖片的時候,會在顯示框(這裡指的不是編輯框而是拿到的editValue)顯示太大。這裡可以利用
網路上也有許多的方法,比如修改ueditor.all.js來,我試過不行,這裡推薦一個簡單易操作的方法,絕對可行。
這裡利用到了JavaScript正則表示式,先利用JavaScript正則表示式來判斷是否是一張圖片,如果是圖片就新增相關的屬性來控制圖片的大小。
//使用正則表示式來判斷是否為圖片,如果為圖片就將其改成適當大大小 //防止圖片太大 if(/<img/.test(msg)){ //msg.replace(/<img/,"<img style='height: 20%;width: 20%;'"); var patt=new RegExp("<img","g");//can be instead of patt=/man/g; msg=msg.replace(patt,"<img style='height: 20%;width: 20%;'"); }
這裡通過替換來實現新增的操作。
這樣在顯示內容的時候就可以控制圖片的大小了,就不會無限制太大了。
另外:附上在編輯框(ueditor)裡控制圖片大小的方法:
ueditor專案目錄下有一個iframe.css檔案,裡面可以自己寫:
寫下以下內容:
/*可以在這裡新增你自己的css*/ img { max-width: 20%; /*圖片自適應寬度*/ max-height: 20%; } body { overflow-y: scroll !important; } .view { word-break: break-all; } .vote_area { display: block; } .vote_iframe { background-color: transparent; border: 0 none; height: 100%; } #edui1_imagescale{display:none !important;}
而後,記得在例項化編輯器中引入該檔案才可以生效:
比如我的:
var ue = UE.getEditor('editor', { toolbars: [ ['redo', 'snapscreen', 'bold', 'horizontal', 'cleardoc', 'fontfamily', 'forecolor', 'fontsize', 'simpleupload', 'insertimage' , 'emotion', 'map', 'insertvideo', 'attachment'] ], iframeCssUrl: '/ueditor/utf8-jsp/themes/iframe.css',// 引入css autoHeightEnabled: false, autoFloatEnabled: true, initialFrameWidth: 500, initialFrameHeight: 100 });
另外,要注意路徑要寫正確。