2016-03-20 20 views
0

私のHTMLとjqueryコードは以下のようになりますが、何らかの理由でスパンのテキストを変更できます。私はfindメソッドとnearestメソッドの両方で試しています。何か案は?おかげjqueryを使用してスパンテキストを検索および変更できません

<body> 
<span class="classA">Text A</span> 
<input id="testId" type="file" name="files" class="fileUp"> 
<span class="classA">Text B</span> 

<script type="text/javascript"> 

    $(document).ready(function() { 
     $("input.fileUp").change(function (event) { 
      $(this).closest('span.classA').text('testing'); 
      $(this).find('span.classA').text('testing'); 

     }); 
    }); 
</script> 
</body> 
+0

使用:$(この).parent()( 'span.classA')を見つけるテキスト( 'テスト');。。 –

答えて

1

span.classA.fileUpの最も近い要素ではありません。次のようにprev()メソッドを使用してください。

$("input.fileUp").change(function (event) { 
 
    $(this).prev('span.classA').text('testing'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<span class="classA">Text A</span> 
 
<input id="testId" type="file" name="files" class="fileUp"> 
 
<span class="classA">Text B</span>

関連する問題