2017-06-05 73 views
0

我試圖從iframe中刪除widthheight屬性,並從我的輸入中刪除所有換行符。所以我做了這個:替換方法不起作用

var convert; 
$(".button1").click(function(){ 
    console.log("done"); 
    var convert = document.getElementById("convert").value; 
    convert = convert.replace(/\r?\n/g, ''); 
    //convert = convert.replace("width=\'500\' height=\'294\'") 
    convert= convert.replace("width=", ''); 
    convert = convert.replace("height=", ''); 
    convert= convert.replace("'500'", ''); 
    convert= convert.replace("'294'", ''); 
    console.log(convert); 
}) 

但它有不一致的結果。有時它有效,有時不起作用。
我也嘗試過註釋掉的部分,但那也行不通。

它總是設法去除換行符,使輸入一行字符串,但它從來沒有設法從大串這樣全殲widthheight屬性:

<li> <h1 class="liname">বীরশ্রেষ্ঠ মোস্তফা কামালঃ-</h1> <div class="iframe"><iframe src="https://www.youtube.com/embed/uZuzPrQ9X_w?&theme=dark&autohide=2&fs=0&rel=0"frameborder="0"></iframe></div> </li> <li> <h1 class="liname">বীরশ্রেষ্ঠ মতিউর রহমান:-</h1> <div class="iframe"><iframe width='500' height='294' src="https://www.youtube.com/embed/u1thP64dhrk?&theme=dark&autohide=2&modestbranding=1&fs=0&showinfo=0&rel=0"frameborder="0"></iframe></div> </li> <li> <h1 class="liname">বীরশ্রেষ্ঠ নূর মোহাম্মদ:-</h1> <div class="iframe"><iframe width='500' height='294' src="https://www.youtube.com/embed/Xjf6CO2bWW8?&theme=dark&autohide=2&modestbranding=1&fs=0&showinfo=0&rel=0"frameborder="0"></iframe></div> </li> <li> <h1 class="liname">বীরশ্রেষ্ঠ মোহাম্মদ রুহুল আমিন :-</h1> <div class="iframe"><iframe width='500' height='294' src="https://www.youtube.com/embed/CpDdMNNkkck?&theme=dark&autohide=2&modestbranding=1&fs=0&showinfo=0&rel=0"frameborder="0"></iframe></div> </li> <li> <h1 class="liname">বীরশ্রেষ্ঠ মুন্সী আবদুর রউফ:-</h1> <div class="iframe"><iframe width='500' height='294' src="https://www.youtube.com/embed/oUgsJSUHZlM?&theme=dark&autohide=2&modestbranding=1&fs=0&showinfo=0&rel=0"frameborder="0"></iframe></div> </li> <li> <h1 class="liname">বীরশ্রেষ্ঠ হামিদুর রহমান :-</h1> <div class="iframe"><iframe width='500' height='294' src="https://www.youtube.com/embed/74hSM6rGwq4?&theme=dark&autohide=2&modestbranding=1&fs=0&showinfo=0&rel=0"frameborder="0"></iframe></div> </li> <li> <h1 class="liname">বীরশ্রেষ্ঠ জাহাঙ্গীর :-</h1> <div class="iframe"><iframe width='500' height='294' src="https://www.youtube.com/embed/ctVFVaAmG7w?&theme=dark&autohide=2&modestbranding=1&fs=0&showinfo=0&rel=0"frameborder="0"></iframe></div> </li> 

的上面的代碼行是刪除換行符後的輸入。

實際的HTML代碼換行符低於:

<li> 
    <h1 class="liname">বীরশ্রেষ্ঠ মোস্তফা কামালঃ-</h1> 
    <div class="iframe"><iframe src="https://www.youtube.com/embed/uZuzPrQ9X_w?&theme=dark&autohide=2&fs=0&rel=0"frameborder="0"></iframe></div> 
</li> 
<li> 
    <h1 class="liname">বীরশ্রেষ্ঠ মতিউর রহমান:-</h1> 
    <div class="iframe"><iframe width='500' height='294' src="https://www.youtube.com/embed/u1thP64dhrk?&theme=dark&autohide=2&modestbranding=1&fs=0&showinfo=0&rel=0"frameborder="0"></iframe></div> 
</li> 
<li> 
    <h1 class="liname">বীরশ্রেষ্ঠ নূর মোহাম্মদ:-</h1> 
    <div class="iframe"><iframe width='500' height='294' src="https://www.youtube.com/embed/Xjf6CO2bWW8?&theme=dark&autohide=2&modestbranding=1&fs=0&showinfo=0&rel=0"frameborder="0"></iframe></div> 
</li> 
<li> 
    <h1 class="liname">বীরশ্রেষ্ঠ মোহাম্মদ রুহুল আমিন :-</h1> 
    <div class="iframe"><iframe width='500' height='294' src="https://www.youtube.com/embed/CpDdMNNkkck?&theme=dark&autohide=2&modestbranding=1&fs=0&showinfo=0&rel=0"frameborder="0"></iframe></div> 
</li> 
<li> 
    <h1 class="liname">বীরশ্রেষ্ঠ মুন্সী আবদুর রউফ:-</h1> 
    <div class="iframe"><iframe width='500' height='294' src="https://www.youtube.com/embed/oUgsJSUHZlM?&theme=dark&autohide=2&modestbranding=1&fs=0&showinfo=0&rel=0"frameborder="0"></iframe></div> 
</li> 
<li> 
    <h1 class="liname">বীরশ্রেষ্ঠ হামিদুর রহমান :-</h1> 
    <div class="iframe"><iframe width='500' height='294' src="https://www.youtube.com/embed/74hSM6rGwq4?&theme=dark&autohide=2&modestbranding=1&fs=0&showinfo=0&rel=0"frameborder="0"></iframe></div> 
</li> 
<li> 
    <h1 class="liname">বীরশ্রেষ্ঠ জাহাঙ্গীর :-</h1> 
    <div class="iframe"><iframe width='500' height='294' src="https://www.youtube.com/embed/ctVFVaAmG7w?&theme=dark&autohide=2&modestbranding=1&fs=0&showinfo=0&rel=0"frameborder="0"></iframe></div> 
</li> 
+0

什麼'convert'你在這裏提到嗎? document.getElementById(「convert」) –

+0

您能否提供[fiddle](https://jsfiddle.net/)? –

+0

實際上是否從iframe中移除高度和寬度的目標是?或者是返回iframe的HTML的目標,但沒有高度和寬度?對於前者,您可以使用jQuery通過刪除寬度和高度屬性來完成繁重的工作;對於後者,你需要.replace()或類似的。 – manassehkatz

回答

0

我不,你怎樣的代碼,但所有你需要做的是:

$(".button1").click(function(){ 
    $('iframe').removeAttr('width').removeAttr('height'); 
}) 
+0

或者使用JavaScript本地方法[removeAttribute()](https://www.w3schools.com/jsref/met_element_removeattribute.asp)。 – myfunkyside