我想創建一個類似於jQuery中的示例here的懸停。但鏈接是動態生成所以我真的很難搞清楚這一點。將鼠標懸停在生成的動態鏈接上
我嘗試這樣做:
$(document).ready(function() {
$('a.g-tabs').on('hover', 'a', function() {
$(this).append($('<i class="icon-clear-remove" onClick="tabRemove();"></i>'));
},
function() {
$(this).find(".icon-clear-remove:last").remove();
});
});
但它不工作。看起來像我的選擇器是問題。我怎樣才能正確選擇它?
UPDATE:爲視圖
這個js是把手不刷新,如果標籤被創建:
$(document).on('submit','#pop-form', function(e) {
// make an ajax request
$.post('../admin/FLT_add_tab.do',
$('#pop-form').serialize(),
function(data) {
// if data from the database is empty string
if($.trim(data).length != 0) {
// hide popover
$('#popover').popover('hide');
//append new tab and new tab content
var id = $(".nav-tabs").children().length - 1;
$('#popover').closest('li').before('<li><a href="#tab_'+ id +'" data-toggle="tab" class="g-tabs">' + data + '</a></li>');
$('.tab-content').append('<div class="tab-pane" id="tab_' + id + '"> <c:import url="flt-pis.html"></c:import> </div>');
} else {
// error handling later here
}
}
);
e.preventDefault();
});
沒有這一條是,如果用戶進入到處理該選項卡中的HTML此頁面中的第一手資料:
<!-- Other tabs from the database -->
<c:forEach var="tabNames" items="${ allTabs }">
<li><a href="#" data-toggle="tab" class="g-tabs"> ${ tabNames.key }</a></li>
</c:forEach>
<!-- Add new tab -->
<li><a href="#" id="popover">New <i class="icon-plus-sign"></i></a></li>
按照要求在服務器端代碼:
@ResponseBody
@RequestMapping(value = "/admin/FLT_add_tab", method = RequestMethod.POST)
public String createNewTab(@RequestParam
String newTab, HttpServletRequest request)
{
HttpSession session = request.getSession();
String returnVal = Credentials.checkSession(session);
if(returnVal != null)
{
return returnVal;
}
String tabName = null;
try
{
DataSource dataSource = DatabaseCommunication.getInstance().getDataSource();
QuestionnaireDAO qDAO = new QuestionnaireDAO(dataSource);
if(qDAO.getTabName(0, newTab) == null)
{
qDAO.insertQtab(newTab);
tabName = newTab;
}
}
catch(Exception e)
{
// no logger yet
e.printStackTrace();
}
return tabName;
}
你能發佈你的html嗎? – dax
你可以做jsFidle的例子嗎? – Khamidulla
@DhavalMarthak它作爲'append'可以取得'jquery object'的參數沒有問題,參見http://api.jquery.com/append/ –