Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在单击链接时运行PHP脚本,无需刷新页面_Php_Javascript_Jquery_Styleswitching - Fatal编程技术网

在单击链接时运行PHP脚本,无需刷新页面

在单击链接时运行PHP脚本,无需刷新页面,php,javascript,jquery,styleswitching,Php,Javascript,Jquery,Styleswitching,好的,我用php做了一个样式切换程序,我在我的网站上使用它作为一个隐藏功能,当用户输入“teema”时会触发,页面内容顶部会出现一个模式框,显示可用的主题 <ul> <li><a href="teema.php?teema=default" class="ajaxLink">Normaali</a></li> <li><a href="teema.php?teema=superhero" class="

好的,我用php做了一个样式切换程序,我在我的网站上使用它作为一个隐藏功能,当用户输入“teema”时会触发,页面内容顶部会出现一个模式框,显示可用的主题

<ul>
    <li><a href="teema.php?teema=default" class="ajaxLink">Normaali</a></li>
    <li><a href="teema.php?teema=superhero" class="ajaxLink">Superhero</a></li>
    <li><a href="teema.php?teema=spacelab" class="ajaxLink">Spacelab</a></li>
    <li><a href="teema.php?teema=united" class="ajaxLink">United</a></li>
    <li><a href="teema.php?teema=cyborg" class="ajaxLink">Cyborg</a></li>
</ul>
但是,我想在不刷新页面的情况下更改主题,这样css应该在后台加载。下面是teema.php:

<?php
    $style = $_GET['teema'];
    setcookie("teema", $style, time()+604800);
    if(isset($_GET['js'])) {
        echo $style; 
    } 
    else{
    header("Location: ".$_SERVER['HTTP_REFERER']);  
    }
?>

以及检查cookie的索引上的php:

<?php 
if(!empty($_COOKIE['teema'])) $style = $_COOKIE['teema'];
else $style = 'default';
?>
<link rel="stylesheet" href="css/<? echo $style;?>.css" id="theme">


您可以在PHP文件中给出JavaScript内容类型的响应,并操作样式表

按以下方式更改代码: 在PHP中:

header("Content-type: text/javascript");
$('#theme').attr('href', 'css/<?php echo $_GET['teema']; ?>');
标题(“内容类型:text/javascript”);
$('theme').attr('href','css/');

更改此链接的内容(
$('$#主题“).attr('href','css/'+selectedtheme+'.css');
,您也可以在javascript中设置cookie(
document.cookie='theme='+selectedtheme

您只需发出ajax请求,cookie就会返回

使用jQuery:

$('.ajaxLink').click(function(e) {
    e.preventDefault();
    var teemaUrl=this.href;
    var teema=teemaUrl.split('=').pop()
    $.get(teemaUrl, { js:true}, function(data){
       $('#theme').attr('href','css/'+ teema+'.css')

    })
});

这将解析链接
href
中的
teema
值,调用该
href
(可能需要添加路径信息)并更新ID=theme的link标签的
href
。@ChristianNikkanen添加了完整答案。很抱歉,错误地按了
Ctrl+Enter
。是的,但这只是指向并包含document.getElementById(“link”).setAttribute(“href”,“teema.css”)是的,我使用jQuery,因为我在标签中添加了它,并询问如何使用jQuery。哇……你能给我一点时间,让我给你一个更新的解决方案吗?
$。getScript
不适合html响应,也不添加php中需要的
js
参数。另外
link
不是IDNope,它肯定会阻止加载,但它不会在不重新加载的情况下替换css。您现有的链接标记是否有ID?
alert($(“#主题”).length)
您的意思是要更改css吗?哎呀,我犯了一个错误
$(“#主题”).attr('href','css/'+teema+.css'))
。我有错误的属性,并且忘记了路径和扩展最完美!是否可以将新css淡入其中?
header("Content-type: text/javascript");
$('#theme').attr('href', 'css/<?php echo $_GET['teema']; ?>');
$('.ajaxLink').click(function(e) {
    e.preventDefault();
    var teemaUrl=this.href;
    var teema=teemaUrl.split('=').pop()
    $.get(teemaUrl, { js:true}, function(data){
       $('#theme').attr('href','css/'+ teema+'.css')

    })
});