2016-03-29 74 views
0

Jquery .append在firefox或safari中無法正常工作,但.val的確如此。.append無法在Firefox或safari中工作

有趣的是,相同的代碼在IE中正常工作。

代碼:

<head> 
    <link rel="stylesheet" type="text/css" href=" https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.3/normalize.min.css"> 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pure/0.6.0/pure-min.css"> 
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script> 
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> 
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 

<script type = "text/javascript"> 
    $(document).ready(function() {   
     $("#notes").change(function() { 
      $('#notes').val($('#notes').val() + "Test1"); 
      $('#notes').append('Test2'); 
     });    
    }); 
</script> 

<textarea rows="10" name="Notes1" id="notes" style="width: 100%" ><?php 
    if (isset($_SESSION['order'])) { 
     echo $_SESSION['order'][0]['tNotes']; 
    } 
</textarea> 

所以上面的代碼工作正常被添加到Internet Explorer中的文字區域,但只有兩個.VAL TEST1和Test2的Test1的工作在FF /野生動物園.append不。

這是爲什麼?任何幫助或替代方案以獲得相同的結果(將文本附加到編輯的地方,而不僅僅是底部)

回答

1

編輯:

(追加文本已編輯,而不只是底部的地方)

使用val(),使其工作在每一個瀏覽器。

See this updated fiddle

$(document).ready(function() { 
    $("#notes").change(function() { 
    insertAtCaret($(this).prop('id'),"this"); 
    $(this).val($('#notes').val() + "here"); 
    }); 
}); 

注意 - 我使用函數從該SO職位: Inserting a text where cursor is using Javascript/jquery

+0

不,我的代碼解釋說,一個適用於所有瀏覽器(.val),但只適用於IE(.append) – user2229747

+1

好吧,我更新了我的回答 –

+0

你我的朋友很棒 – user2229747

0

Textarea是一種輸入容器類型,因此您必須使用.val()方法來設置值你的元素。因爲IE不關心標準,所以.append()在IE中工作。

+0

好,謝謝,有沒有辦法讓.VAL()將文本添加到最後改變textarea的行(如.append)? – user2229747

+0

.val()是一個getter和一個setter。 沒有參數,它通過參數設置。 –

相關問題