Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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
Jquery 替换复杂标题中的样式表_Jquery_Html_Css_Stylesheet - Fatal编程技术网

Jquery 替换复杂标题中的样式表

Jquery 替换复杂标题中的样式表,jquery,html,css,stylesheet,Jquery,Html,Css,Stylesheet,我有一个网站,我需要创建一个样式表切换。现在,当涉及到html/css/js的所有事情时,我几乎是自学成才的,所以请事先原谅我的新手。我修改了一些代码以满足我的需要,但我似乎找不到如何更改代码以针对头部中的特定行 目前我拥有的是: <ul id="nav"> <li><a href="#" rel="http://files.enjin.com/149304/theme-dark.css">Dark</a></li> &

我有一个网站,我需要创建一个样式表切换。现在,当涉及到html/css/js的所有事情时,我几乎是自学成才的,所以请事先原谅我的新手。我修改了一些代码以满足我的需要,但我似乎找不到如何更改代码以针对头部中的特定行

目前我拥有的是:

<ul id="nav">
    <li><a href="#" rel="http://files.enjin.com/149304/theme-dark.css">Dark</a></li>
    <li><a href="#" rel="http://files.enjin.com/149304/theme-light.css">Light</a></li>
</ul>

<script>
$(document).ready(function() { 
    $("#nav li a").click(function() { 
        $("link").attr("href",$(this).attr('rel'));
        return false;
    });
});

$(document).ready(function() { 
    if($.cookie("css")) {
        $("link").attr("href",$.cookie("css"));
    }
    $("#nav li a").click(function() { 
        $("link").attr("href",$(this).attr('rel'));
        $.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
        return false;
    });
});

$(document).ready(function() { 
    if($.cookie("css")) {
        $("link").attr("href",$.cookie("css"));
    }
    $("#nav li a").click(function() { 
        $("link").attr("href",$(this).attr('rel'));
        $.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
        return false;
    });
});
</script>
$(文档).ready(函数(){ $(“#nav li a”)。单击(函数(){ $(“link”).attr(“href”,$(this.attr('rel')); 返回false; }); }); $(文档).ready(函数(){ 如果($.cookie(“css”)){ $(“link”).attr(“href”,“$.cookie”(“css”); } $(“#nav li a”)。单击(函数(){ $(“link”).attr(“href”,$(this.attr('rel')); $.cookie(“css”,$(this.attr('rel'),{expires:365,path:'/'}); 返回false; }); }); $(文档).ready(函数(){ 如果($.cookie(“css”)){ $(“link”).attr(“href”,“$.cookie”(“css”); } $(“#nav li a”)。单击(函数(){ $(“link”).attr(“href”,$(this.attr('rel')); $.cookie(“css”,$(this.attr('rel'),{expires:365,path:'/'}); 返回false; }); });
就目前而言,这段代码只是更改了头部的每一个href并破坏了页面

这是我需要更改的href(它位于第22行):



非常感谢您的帮助。

在源代码中,您不能通过DOM元素的行号来遍历DOM元素

您需要使用一些技术来区分需要更改的链接标记。一个简单的建议是使用自定义属性

例如:

<link xtype="1" href="./Home - Oceanian Gaming Community_files/theme.php"  ... >
以上是实现此目的所需的唯一现成处理程序


一个稍有风险的解决方案是使用属性包含选择器来匹配特定的链接标记。风险在于,这可能会根据页面上的内容匹配多个链接标签

$("link[href*="/theme.php"]")

查看网站后编辑

好吧,有几件事是不正确的

首先,引号是错误的-您需要$('link[href*=“/theme.php”]”),即模式的单外双内(反之亦然,但两个位置的引号不相同)

其次,更改一次后,模式href*=“/theme.php”将不再匹配,因为我们更改了href属性。因此,在第一次更改
href
时添加一个自定义属性,并在第二次更改时使用此自定义属性匹配链接标记

if($('link[xtype]').length == 0) {
    // first time - set href and add custom attribute
    $('link[href*="/theme.php"]').attr("href", $(this).attr('rel')).attr('xtype', 1);
}
else {
    // second time on wards
    $('link[xtype]').attr("href", $(this).attr('rel')).attr('xtype', 1);
}

你可以把所有这些都放在一个现成的处理器里。。那会更干净。此外,它还可以帮助您更快地捕获错误。有一种可能的解决方案,但如果存在多个具有类似a href属性的链接标记的可能性,则可能存在风险。请参阅edit.getting the link element using jQuery('link')。eq(number)有帮助?@Kuro-尝试将匹配模式简化为“theme.php”而不是完整的URL。i、 e.
$(“link[href*=”theme.php“])
-这应该可以解决问题。您是否将页面放在URL或小提琴中。“我可以调查一下。”techfoobar——我当然知道。我将样式切换器放置在右侧栏的顶部。
$("link[href*="/theme.php"]")
if($('link[xtype]').length == 0) {
    // first time - set href and add custom attribute
    $('link[href*="/theme.php"]').attr("href", $(this).attr('rel')).attr('xtype', 1);
}
else {
    // second time on wards
    $('link[xtype]').attr("href", $(this).attr('rel')).attr('xtype', 1);
}