2010-01-22 78 views
0
var macs = { 
    getMacAddress : function() 
    { 
     document.macaddressapplet.setSep("-"); 
     document.write("Mac Address = " + document.macaddressapplet.getMacAddress()); 
    } 

我該如何調用此函數並在網站中顯示?javascript調用函數

這似乎並不工作..

<script type="text/javascript"> 
    document.write(mac.getMacAddress()); 
</script> 

誰能幫助?

+3

這是一個錯字嗎? document.write(macs.getMacAddress()); – 2010-01-22 07:24:20

回答

3

只是使用macs.getMacAddress();

函數不返回任何東西。它只是寫入文檔。如果您希望它返回字符串,請在定義macs時將document.write替換爲return

另外,請注意,在頁面加載後調用document.write將會覆蓋當前頁面(因爲它意味着document.open)。如果您需要在文檔加載後附加到該文檔,請使用DOM或innerHTML。

+0

謝謝... 如果我想顯示該網頁上的mac地址本身,我該怎麼辦? 有沒有我想要讀的例子?或者你有樣品給我嗎? – kennedy 2010-01-22 07:33:29

+0

如果您在頁面加載後執行此操作,最簡單的方法是使用具有唯一標識的元素,例如'',並將其內容填充到腳本中:var foo = document。的getElementById( 「MACADDRESS」); foo.appendChild(document.createTextNode(macs.getMacAddress()));',該函數返回字符串('返回「Mac地址=」+ ...')。 如果您以後需要覆蓋它,您可以執行'foo.removeChild(foo.firstChild);'然後追加更多。 – Anonymous 2010-01-22 07:36:42

+0

好吧,明白了。謝謝... 由於我能夠顯示出數據,我如何將它保存到MySQL數據庫? – kennedy 2010-01-22 08:36:59