2
當有人點擊添加圖像時,我想插入一個對象($roleOption
)。 else條件工作正常,並且完全如我所願。jQuery對象根本不需要預先設置
但是,第一個條件(「如果沒有選項,直接在圖像之前插入」)根本不起作用。沒有錯誤被觸發,它只是不會預先考慮對象。
我是否正確使用了前置方法?
<script language="javascript" type="text/javascript">
$(document).ready(function() {
var $roleOption = "<div class='roleoption'><img src='@Url.Content("~/Public/images/delete.png")' alt='delete' />@Html.Raw(@DSS.WebUI.Helpers.CustomHelpers.SelectListItemsForRoleJavascript(Model.ExistingRoles, 1))</div>";
$('img.add-role').click(function() {
if ($(this).siblings('.roleoption').length == 0) {
//Displaying zero as expected. So it's entering this conditional.
console.log($(this).siblings('.roleoption').length);
$(this).prepend($roleOption); //However the element isn't being prepended. :/
}
else {
$($roleOption).insertAfter($(this).parent().find('.roleoption:last')); //This works as expected.
}
});
});
</script>
啊我看到了,文檔讓我相信prepend會在「this」之前添加元素。圖像之前的含義,不在其內部。謝謝你解決了我的問題。 – 2012-02-01 03:16:50