これらの類似した JS コード (すぐ下の 2 つ) を使用すると、出力が間違っているか、表示されません。問題のコードは次のとおりです。
if (location.href != "website.com/page1" || "website.com/page2")
{
element.style.backgroundColor='none';
}
または != と !== を使用
if (location.href != "website.com/page1" || location.href != "website.com/page2")
{
element.style.backgroundColor='none';
}
これが他のコードでの使用方法です(簡略化)
<script>
var element = document.getElementbyId('idElement');
if (location.href != "website.com/page1")
{
element.style.backgroundColor='blue';
}
if (location.href != "website.com/page2")
{
element.style.backgroundColor='green';
}
//HERE'S WHERE I PUT IT
if (location.href != "website.com/page1" || "website.com/page2")
{
element.style.backgroundColor='none';
}
</script>
何も起こらないか、要素が他のページで正しく機能しません。
私は何を間違っていますか?
詳細情報: Tumblr のテーマを作成していますが、別のページで表示すると、異なる特徴を持つ特定の投稿が含まれるページがあります。このコードを一番上に置く必要があります。
誰かがこれを提案しました: 解決済み
<script>
window.onload = function ()
var element = document.getElementbyId('idElement');
if (location.href != "website.com/page1" && location.href != "website.com/page2")
{
element.style.backgroundColor='none';
}
if (location.href == "website.com/page1")
{
element.style.backgroundColor='blue';
}
if (location.href == "website.com/page2")
{
element.style.backgroundColor='green';
}
</script>