2014-03-06 28 views
0

我有以下的javascriptEcho在一個JavaScript

function showIt(item,i,j,max){ 
    var id; 
    actualItem=item; 
    for(var x=1;x<=i;x++){ 
    id=item+"_"+x; 
    document.getElementById(id).src="<?= suburl(); ?>/b.gif"; 
    } 
    for(var x=i+1;x<=max;x++){ 
    id=item+"_"+x; 
    if(x<=j) 
     document.getElementById(id).src="<?= suburl(); ?>/y.gif"; 
    else document.getElementById(id).src="<?= suburl(); ?>/w.gif"; 
    } 
} 

我與<?= suburl(); ?>並用<?php echo suburl(); ?>

功能suburl()嘗試了PHP函數;看起來像這樣:

function suburl() { 
    $suburl = 'http://localhost/www.site.com/static'; 
    echo $suburl; 
} 

它不工作在javascript中回顯它。它看起來像純html。 非常感謝你。

+1

你不能做到這一點,PHP是服務器端,它不會將您的客戶端上執行。您可以改爲使用PHP打印所需URL的Javascript功能。 – skywalker

+0

嘗試'功能suburl(){ $ suburl ='http://localhost/www.site.com/static'; return $ suburl; }' –

+0

你有'.php'頁面上的這段代碼嗎?否則它將無法工作。 – vonUbisch

回答

1

你可以用這樣的嘗試:

function showIt(item,i,j,max){ 
    var id; 
    actualItem=item; 
    var url = suburl(); //THIS IS NEW 
    for(var x=1;x<=i;x++){ 
    id=item+"_"+x; 
    document.getElementById(id).src=url+"/b.gif"; 
    } 
    for(var x=i+1;x<=max;x++){ 
    id=item+"_"+x; 
    if(x<=j) 
     document.getElementById(id).src=url+"/y.gif"; 
    else document.getElementById(id).src=url+"/w.gif"; 
    } 
} 

function suburl() { 
    var suburl = 'http://localhost/www.site.com/static'; 
    return suburl; 
} 
-5
function showIt(item,i,j,max){ 
var id; 
actualItem=item; 
for(var x=1;x<=i;x++){ 
id=item+"_"+x; 
document.getElementById(id).src="<?= echo suburl(); ?>/b.gif"; 
} 
for(var x=i+1;x<=max;x++){ 
    id=item+"_"+x; 
    if(x<=j) 
    document.getElementById(id).src="<?= echo suburl(); ?>/y.gif"; 
else document.getElementById(id).src="<?= echo suburl(); ?>/w.gif"; 
} 
} 

函數suburl();看起來像這樣:

function suburl() { 
$suburl = 'http://localhost/www.site.com/static'; 
return $suburl; 
} 

希望這個幫助。 感謝

+0

我希望你不要指望這真的起作用,是嗎? –

+0

我什至不知道你改變了什麼。也許你應該描述你做了什麼。也許。 – Butt4cak3

+0

他在php標籤中添加了回聲。 –