2014-03-19 185 views
0

我想在jQuery中鏈接一個函數,並有問題。jQuery問題鏈接

http://jsfiddle.net/Aa7S9/7/

基本上,語法是:

$("#newsletter").click(function() { 
    $(this).replaceWith('html stuff') 
.$("#transition").focus(); 
}); 

任何理由爲什麼會這樣呢?我replaceWith工作,並返回一個jQuery對象,因此應該與鏈接工作。

(實際上,我想在JSFiddle中將輸入注入DOM後的輸入文本框中)。

回答

1

你可能需要這個 我已經更新您的JS 請點擊這裏 http://jsfiddle.net/Aa7S9/10/

$("#newsletter").click(function() { 
$(this).replaceWith('<div class="input-group"><input type="text" class="pull-right form-  control input-lg" id="transition"><span class="input-group-btn"><button type="submit" class="btn btn-lg btn-warning">Submit <span class="glyphicon glyphicon-envelope"></span>  </button></span></div>'); 

$( '#轉換')專注()。 });

+0

是的,這也是我最後的結局。謝謝。 –

2

.alert()不是一個jQuery方法,你需要做的:

$("#newsletter").click(function() { 
    $(this).replaceWith('html stuff'); 
    alert("this isn't firing"); 
}); 

你可以只觸發focus()input元素本身:

$("#newsletter").click(function (e) { 
    e.preventDefault(); 
    var content = '<div class="input-group"><input type="text" class="pull-right form-control input-lg" id="transition"><span class="input-group-btn"><button type="submit" class="btn btn-lg btn-warning">Submit <span class="glyphicon glyphicon-envelope"></span></button></span></div>' 
    $(this).replaceWith(content); 
    $('#transition').focus(); 
}); 

Updated Fiddle

+0

您的代碼是正確的,但我更新了我的帖子,因爲我確實試圖使用focus()專注於創建的新文本輸入。我希望現在能夠正確顯示在我的示例中。 –

+0

請檢查我的更新:-) – Felix