2015-09-25 82 views
-1

我想在我的代碼使用jQuery改變。我是初學者。我試圖用函數「.replaceWith()」來做到這一點。 我首先嚐試將顏色更改爲紅色。 我所做的: http://jsfiddle.net/kzVSt/300/按鈕被其他2個按鈕

$(document).ready(){ 
 
    $("#del").click(function(){ 
 
    \t $("#del").replaceWith("<input type='button' id='del1' value='dsjkdsa'>"); 
 
    }); 
 
};
#del1{ 
 
    color:red; 
 
}
<input type="button" id="del" value="button">

+0

片段的改變ID的作品:http://jsfiddle.net/kzVSt/301/ –

+0

FYI:在你的HTML按鈕ID爲「DEL」,但在你的CSS你把它當作「 DEL1" – Andrea

+0

這是因爲該CSS規則是新元素,他與JavaScript –

回答

1

使用$(document).ready(function() { ... });

嘗試

$(document).ready(function() { 
 
    $("#del").click(function(){ 
 
    \t $("#del").replaceWith("<input type='button' id='del1' value='dsjkdsa'>"); 
 
    }); 
 
});
#del1{ 
 
    color:red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="button" id="del" value="button">

+0

哦,上帝,我不記得有關「功能」。謝謝! – patwoj98

0

ready功能的正確語法如下:

$(document).ready(function(){ 
    // … 
}); 
0

好像你正在使用的ID和類不正確。這裏有幾個技巧

代替:

$(document).ready(function(){ 

別名它

jQuery(function($){ 

這是一個簡碼的jQuery別名。就緒$()處理。

接下來,如果你與其他元素取代的元素,綁定到該元素的所有事件將被銷燬。相反,切換類。

如果你要更新的價值,目標在jQuery對象上.val()方法(您可以通過$(this)引用它們的事件中的元素。

最後,當你想添加一些CSS顏色使用類,而不是。

jQuery(function(){ 
    $('#del').on('click', function(){ 
     $(this).addClass('color-red'); 
     $(this).val('dsjkdsa'); 
    }); 
}); 
相關問題