2013-01-17 105 views
-1

基本上我使用的SCEditor,並希望在論壇帖子或文章評論中稱爲「引用」的按鈕,它從信息中抓取文本並將其粘貼到textarea中,我有此代碼:使用javascript填充文本區域

<script type="text/javascript"> 
jQuery.noConflict(); //disable global $ for jQuery 
jQuery(document).ready(function($){ 
    $(function(){ 
    $('#quote').click(function() { 
     $('textarea').sceditor('instance').insert("[quote=admin]Some text[/quote]"); 
    }); 
    }); 
}); 
</script> 

現在,它會把「[quote = admin]一些文本[/ quote]」放到textarea中。問題是我需要以某種方式進行動態變化,所以如果這有意義,我不必在每個評論上面複製該代碼。所以,而不是這個:

insert("[quote=admin]Some text[/quote]"); 

該部分將抓住從下面的框中的所有文本呢?而不是我手動將帖子中的文字替換爲引用的位。

那麼基本上點擊引用鏈接基本上取代上面引用的位與不同div的內容?

+0

這樣像你一樣正在朝着解決方案邁進。什麼是問題?我不明白你在問什麼。 –

+0

你能澄清你的問題嗎?你也可以發佈html代碼或用於生成它的腳本? – SnareChops

+0

我的問題是我如何用元素內容替換「[quote = admin]一些文本[/ quote]」?像一個div例如? – NaughtySquid

回答

0

你可以在這個簡單的例子

<textarea id="ta" rows="5" cols="20"> 
    [quote=admin]Some text[/quote] 
</textarea> 
<br /> 
<input type="button" value="Click Me" onclick="rep()" /> 
<br /> 
<div id="dv" style="width:100px; height:100px; border:black 1px solid;"> 
    Some div content 
</div> 

<script type="text/javascript"> 
    function rep() { 
    var tatxt = $("#ta").val(); 
    var dvtxt = $("#dv").html(); 
    var newtxt = tatxt.replace(/\[quote\=admin\].*?\[\/quote\]/ig, dvtxt); 
    $("#ta").val(newtxt); 
    } 
</script> 

You can see it work here使用正則表達式等。

0

這取決於您的條目的結構。假設「quote」按鈕和div id =「entry」位於嵌套在父級或其他容器中的同一級別上。

<div id="wholeThing"> 
    <div id="entry"> ....... </div> 
    <!-- other stuff goes here --> 
    <input type="button" name="quote" ......> 
</div> 

然後你就可以用這個去:

$(this).parent().find("#entry").text(); 

如果你確切地知道內容所在,你可以在層次結構中的前一元素進行迭代。

$(this).prev().prev().text(); // if content container is 2 elements in front 

對於按鈕單擊處理程序,你可以設置一個類或使用其名稱屬性:

$('div input[name="quote"]').click(function() { // for name attribute 
    $('textarea').sceditor('instance').insert("[quote=admin]Some text[/quote]"); 
}); 

以一級等於所有的「報價」按鈕:

$('.className').click(function(){ ... 

如果是每個條目中唯一的輸入元素:

$('div input').click(function() { ...