gpt4 book ai didi

javascript - 如何修改 javascript 文件中的 indexof 以在外部站点加载的文件上使用

转载 作者:行者123 更新时间:2023-12-02 22:49:07 25 4
gpt4 key购买 nike

我找到了一个我想使用的脚本,但它在语言中使用了indexof,我将从外部站点加载样式表。

这是我正在使用的下载的 HTML 文件

<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="alternate stylesheet" type="text/css" href="stylealt1.css" title="alternate 1" />
<link rel="alternate stylesheet" type="text/css" href="stylealt2.css" title="alternate 2" />

<script type="text/javascript" src="styleswitcher.js"></script>


<a href="#" onclick="setActiveStyleSheet('default'); return false;"><img src="defaultstyle.jpg" alt="default style" /></a>
<a href="#" onclick="setActiveStyleSheet('alternate 1'); return false;"><img src="altstyle1.jpg" alt="alternate style 1" /></a>
<a href="#" onclick="setActiveStyleSheet('alternate 2'); return false;"><img src="altstyle2.jpg" alt="alternate style 2" /></a>

我的样式表将从不同的网址加载,例如

<link rel="stylesheet" type="text/css" href="www.mysite.com/style.css" />
<link rel="alternate stylesheet" type="text/css" href="www.mysite.com/stylealt1.css" title="alternate 1" />
<link rel="alternate stylesheet" type="text/css" href="www.mysite.com/stylealt2.css" title="alternate 2" />

如何更改“styleswitcher.js”文件,以便在加载默认和备用的外部样式表时起作用,我假设使用indexof,需要替换。

function setActiveStyleSheet(title) {
var i, a, main;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
}

function getActiveStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
}
return null;
}

function getPreferredStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1
&& a.getAttribute("rel").indexOf("alt") == -1
&& a.getAttribute("title")
) return a.getAttribute("title");
}
return null;
}

function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

window.onload = function(e) {
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
}

window.onunload = function(e) {
var title = getActiveStyleSheet();
createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

最佳答案

你混淆了 indexOf,它 is a method on the string prototype ,与目录索引,完全不相关。

按原样,您发布的 JS 中没有任何内容会阻止您将其与外部样式表一起使用。事实上,这是我整理的一个示例,显示它使用了两个外部托管的样式表:

function toggleStyles() {
var activeStylesheetTitle = getActiveStyleSheet();
var toggleTo = activeStylesheetTitle === 'Bootstrap' ? 'Materialize' : 'Bootstrap';
setActiveStyleSheet(toggleTo);
}
function setActiveStyleSheet(title) {
var i, a, main;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
}

function getActiveStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
}
return null;
}

function getPreferredStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1
&& a.getAttribute("rel").indexOf("alt") == -1
&& a.getAttribute("title")
) return a.getAttribute("title");
}
return null;
}

function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

window.onload = function(e) {
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
}

window.onunload = function(e) {
var title = getActiveStyleSheet();
createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
<link rel="stylesheet" title="Bootstrap" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" />
<link rel="alternate stylesheet" title="Materialize" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" />
<div style="font-size:20px; text-align:center; margin-bottom:10px; background-color:red; color:white; text-decoration: underline;" onclick="toggleStyles()">Click me to toggle stylesheets</div>
<button>Button Example</button>
<p>Paragraph Text Example: </p>
<select class="browser-default">
<option>Option A</option>
<option>Option B</option>
</select>
<p>Input field example:</p>
<div class="input-field">
<input type="text" value="type in me!" />
</div>

关于javascript - 如何修改 javascript 文件中的 indexof 以在外部站点加载的文件上使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58259190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com