2015-12-15 52 views
0

我有多個width="1" height="1"的html。我想用border-image: none來代替它們。正則表達式多個引號標記

我已經試過:

$printCanvas.html().replace(new RegExp("width=\"1\" height=\"1\"", 'g'), "border-image: none"); 

,似乎沒有奏效。有什麼建議麼?

-----更新------

對於那些誰問,爲什麼我不需要style標籤:

其實,我是想處理它通過GoogleMap的生成的HTML在EdgeIE10。我正在使用庫,它將jQuery div轉換爲圖像,它在Edge中工作,但不在IE10中。即,html2canvas。我在這裏的輸出:issue with IE10, IE9

後仔細比較HTML,我看到的唯一區別是如下:

IE10,它有width="1" height="1"標籤:

<div style="width: 66px; height: 26px; cursor: pointer;"><img width="1" height="1" style="margin: 0px; padding: 0px; border: 0px currentColor; left: 0px; top: 0px; width: 66px; height: 26px; position: absolute; -ms-user-select: none;" draggable="false" src="https://maps.gstatic.com/mapfiles/api-3/images/google4.png"> 

Edge

<div style="width: 66px; height: 26px; cursor: pointer;"><img style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; left: 0px; top: 0px; width: 66px; height: 26px; position: absolute; -ms-user-select: none;" draggable="false" src="https://maps.gstatic.com/mapfiles/api-3/images/google4.png"> 

我在想使用正則表達式將所有width="1" height="1"更改爲border-image:none

printCanvas.html().replace(new RegExp("width=\"1\"\\s+height=\"1\"", 'g'), "border-image: none"); 

,然後問html2canvas庫來處理它:

window.html2canvas(printCanvas, { 
    useCORS: true, 
    onrendered: function(canvas) { 
     const img = canvas.toDataURL("image/png");    
     base64Img = img; 
     base64Img = base64Img.replace('data:image/png;base64,', ''); 
     resolve(base64Img); 
    } 
    }); 

,並沒有發生任何變化....仍然會在IE10空白。

下面

在兩個瀏覽器對比所呈現的HTML屏幕:

enter image description here

+0

你有沒有嘗試過寬度和高度屬性之間的空間? – MIE

+0

你能提供一個jsfiddle鏈接嗎? – Thinker

+2

用你當前的例子,你得到了printCanvas的html,然後替換了css樣式(width/height數據屬性),但是'replace'返回的值沒有被分配給$ printCanvas的html。你的代碼也不會有你想要的效果。 'border-image:none'需要插入到嵌入式樣式塊中,而不是插入到元素的數據屬性中。 –

回答

3

您有多個問題。

  1. html().replace()返回修改後的字符串,但不會將其分配回打印畫布的html。 (即$printCanvas.html(resultString);)。
  2. 您不能用數據屬性width=1px替換border-image css樣式。

推薦的方法:

查找具有1的寬度和1的高度(數據屬性)的元素。 JQuery的一個行方式(如果指定像素):

$('*[width="1px"]*[height="1px"]').css('border-image', 'none'); 

如果不指定在HTML像素(這我假設基於關閉您的代碼):

$('*[width="1"]*[height="1"]').css('border-image', 'none'); 

例小提琴: https://jsfiddle.net/madc9b25/

+0

感謝您的解決方案,在調查我的問題後,我覺得我的問題可能與其他問題有關,而不是正則表達式......但這種解決方案只是幫助我瞭解更多。 – JavaScripter

+0

肖恩,他試圖將寬度和高度替換爲0。否則很好。 – HTDE

+0

檢查html和css是由Googlemap生成的。檢查這個提琴手,http://jsfiddle.net/tsPmt/9/分別在IE10和Edge中運行它並檢查Googlemap部件'div id =「map-canvas」',你會看到兩個瀏覽器返回不同的HTML。我試圖讓所有IE10呈現的代碼看起來類似於Edge來解決我的問題。 – JavaScripter