我想爲我的web應用程序提供一種方法,允許用戶在他的代碼中的任何位置(腳本標記內)調用我的函數,以顯示淡入/淡出消息。我不知道該怎麼做才能確定我在DOM中的位置,而沒有起始參考點。如何在沒有起始參考點的情況下獲得父節點?
功能:
function displayMessage(message) {
// Display a notification if the submission is successful.
$('<div class="save-alert">' + message + '</div>')
.insertAfter($('')) // Do not know how to know where to put the message.
.fadeIn('slow')
.animate({ opacity: 1.0 }, 2000)
.fadeOut('slow', function() {
$(this).remove();
});
}
的HTML:
<!-- Some HTML -->
<div>
<script type="text/javascript">
displayMessage("My message.");
</script>
</div>
<!-- Some more HTML. -->
你的意思是你想確定上下文acoording實際腳本代碼被寫入的位置? – 2011-01-31 22:00:11