2015-10-26 47 views
1

如果我有:jQuery的鏈接自定義函數

function printRangeValue(){ 
    $(this).prev().children("output").text($(this).val()); 
} 
$(document).ready(function(){ 
    //... 
    $("#strStat").printRangeValue(); 
    $("#conStat").printRangeValue(); 
}); 
<label for="strStat">Strength<output id="forStr" for="strStat">12</output></label> 
<input type="range" id="strStat" class="stat" name="strStat" min="3" max="20"> 

有一個簡單的方法來改變準備功能的內容,使$(this)工作?

謝謝!

+1

您是否在尋找'jQuery.fn.printRangeValue =函數(){...}'?或者是其他東西?這有點難以分辨,因爲你的問題是關於「鏈接」的問題,但你實際上並沒有進行任何鏈接。你的代碼不會像書面的那樣工作,但你並沒有提到完全失敗,所以很難確定你想要什麼。 – apsillers

+0

你想[創建你自己的插件](https://learn.jquery.com/plugins/basic-plugin-creation/)。 – moonwave99

+0

好吧,你需要2個或更多的功能稱之爲連鎖?如果是這樣,那麼我肯定是錯誤的鏈接,那裏沒有任何失敗者,它只是沒有工作。 – user5489152

回答

0

jQuery.fn.extend({ 
 
    printRangeValue: function() { 
 
     console.log(this); 
 
     $(this).prev().children("output").text($(this).val()); 
 
    } 
 
}); 
 

 
$(document).ready(function() { 
 
    $("#strStat").printRangeValue(); 
 
    $("#conStat").printRangeValue(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<label for="strStat">Strength 
 
    <output id="forStr" for="strStat">12</output> 
 
</label> 
 
<input type="range" id="strStat" class="stat" name="strStat" min="3" max="20">

編號:http://api.jquery.com/jquery.fn.extend/

+0

演示:http://jsfiddle.net/kishoresahas/z60j538d/ –

+0

它現在工作,謝謝! – user5489152