[英]how do i find and convert link tags to @import tags inside a textarea using jquery and/or javascript?
如標題所示,我有一個文本輸入區域,該區域定義了一些CSS樣式表。 我正在尋找在文本輸入區域內用@import標簽替換所有鏈接標簽的方法。 我需要一些javascript函數,這些函數會觸發點擊圖片的操作。
關於如何構造函數以進行查找和替換的任何想法?
謝謝!
PS這里是一個例子。 我需要以下幾點:
<textarea rows="30" cols="30" name="code">
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
</textarea>
對此:
<textarea rows="30" cols="30" name="code">
@import url("stylesheet.css");</textarea>
var textarea = document.getElementById('pleaseGiveYourTextareaAnID');
textarea.value = textarea.value.replace(new RegExp('<link rel="stylesheet" type="text/css" href="(.*)" />', 'g'), '@import url("$1");');
應該做到的。
只是為了好玩,一個純jQuery版本: http : //jsfiddle.net/JAAulde/E8WW3/3/
var $textarea = $( 'textarea[name="code"]' ); $textarea.val( $( '<div>' ) .html( $textarea.val() ) .find( 'link[rel="stylesheet"][type="text/css"]' ) .each( function() { var $this = $( this ); $this.replaceWith( '@import url("' + $this.attr( 'href' ) + '");' ); } ) .end() .html() );
@import url("stylesheet1.css"); jhg jhjh jhgh hjgk g @import url("stylesheet2.css"); @import url("stylesheet3.css"); b kj jhvh gjfvjk guy m guyyu @import url("stylesheet4.css");vj hj ,mh jkh kjgh @import url("stylesheet5.css"); bhjbhj
<textarea rows="30" cols="30" name="code"> <link rel="stylesheet" type="text/css" href="stylesheet1.css" /> jhg jhjh jhgh hjgk g <link rel="stylesheet" type="text/css" href="stylesheet2.css" /> <link rel="stylesheet" type="text/css" href="stylesheet3.css" /> b kj jhvh gjfvjk guy m guyyu <link rel="stylesheet" type="text/css" href="stylesheet4.css" />vj hj ,mh jkh kjgh <link rel="stylesheet" type="text/css" href="stylesheet5.css" /> bhjbhj </textarea>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.