<script>
function foo(){...}
</script>
<div><script>foo();</script></div>
<span><script>foo();</script></span>
,我想有富回報「ABC」從一個div調用時,返回「123」從跨度調用時。
這可能嗎?
<script>
function foo(){...}
</script>
<div><script>foo();</script></div>
<span><script>foo();</script></span>
,我想有富回報「ABC」從一個div調用時,返回「123」從跨度調用時。
這可能嗎?
閱讀您的評論後,我認爲你應該做點別的:
#ad1_empty
,#ad2_empty
,...)在底部#ad1_full
,#ad2_full
...)//代碼
<div id="ad_empty"><!-- placeholder --></div>
<div id="ad_content" style="display: none;">
<script src="[ad resource]"></script>
<script>inline ad script</script>
</div>
<script>
function replace(oldel, newel, show) {
if (typeof newel == "string")
newel = document.getElementById(newel);
if (typeof oldel == "string")
oldel = document.getElementById(oldel);
if (newel && oldel)
oldel.parentNode.replaceChild(newel, oldel);
if (show)
newel.style.display = "";
}
window.onload = function() {
replace("ad_empty", "ad_content", true);
replace("ad_empty2", "ad_content2", true);
};
</script>
這是可以做到的,你本來想要的東西,順便說一句(但它也沒用)
function foo() {
var scripts = document.getElementsByTagName("script");
var parent = scripts[scripts.length-1].parentNode;
var tag = parent.nodeName.toUpperCase();
if (tag == "DIV") {
alert("called from a <div>");
} else if (tag == "SPAN") {
alert("called from a <span>");
}
}
不,這是不可能的。您將不得不手動傳遞某種參數以確定它從何處被調用。
有沒有辦法做到這一點,腳本可以調用很多不同的方式,只是沒有足夠的需要這樣把它放在spec中的任何方式。
我會想象有一個整體的很多簡單的方式做你想實現什麼,嵌入<script>
各個元素內,立足什麼斷<script>
元素的位置不」似乎是一個合理方法。
那麼......你想在這裏最終完成什麼?
其實,這是可能的。 – SLaks 2010-10-24 16:58:47
@SLaks - 它不是,因爲當前元素還沒有關閉,所以你不能確定你在哪裏(取決於瀏覽器),例如'setTimeout(foo,0);'會返回...什麼? – 2010-10-24 17:00:13
目前使用的廣告管理器(百度的廣告管理器)需要一個內嵌腳本,用於放置廣告。腳本調用'document.write'將廣告放置在頁面上。從海外訪問時,他們的服務器速度很慢,正在減慢頁面速度。我希望能夠在加載完所有內容後將所有內容移動到頁面的底部,但我無法弄清楚如何將腳本的document.write內容放入內聯腳本的調用位置。 – CheapSteaks 2010-10-24 17:06:09
您可以在DOM中獲取當前的最後一個元素。
內嵌<script>
塊中的代碼在瀏覽器分析頁面時執行。因此, 最後一個DOM元素將會在腳本之前。
神聖的廢話我不能相信這個作品。它會在IE6中工作嗎? – CheapSteaks 2010-10-24 17:32:35
@CheapSteaks:我幾乎可以肯定;嘗試一下。 – SLaks 2010-10-24 18:28:51
這是一個好主意,我可能會這樣做。雖然我不能相信第二個作品。它有多糟糕?你知道它是否適用於IE6嗎? – CheapSteaks 2010-10-24 17:40:26
http://jsbin.com/igibu4/它與您提供的輸入一起工作,但我不會將它用於任何嚴重的事情。第一種解決方案被廣泛用於提高頁面加載時間。 – galambalazs 2010-10-24 17:53:22