2014-03-06 24 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标签中添加了回声。 –