通过if else语句更改正文-url

时间:2018-12-20 23:31:50

标签: javascript if-statement getelementbyid

如果url是子域http://one.example.com/dsnknw?211218,则显示div 1,如果http://two.example.com/ppppnw?201218则显示div 2。

每个身体都有背景图片

install.packages("pdftools")
install.packages("tesseract")
install.packages("plyr")
install.packages("qpcR")

library(pdftools)
library(tesseract)
library (plyr)
library(qpcR)
text <- ocr("POC File 12.20 (3).pdf")
test2<-strsplit(text,"\n")
df <- ldply (test2, data.frame)
compile<-df



file_list <- list.files()
for (file in file_list){
 text <- ocr(file)
 test2<-strsplit(text,"\n")
 df <- ldply (test2, data.frame)
 compile<-qpcR:::cbind.na(compile,df)
}
write.csv(compile,"compiled.csv")

和关联的链接:

<style>
#siteone {
background: url(http://example.info/backgroundone.PNG); 
    background-repeat: no-repeat;
    background-size:cover;
    }
#sitetwo {
    background: url(http://example.info/backgroundtwo.PNG); 
    background-repeat: no-repeat;
    background-size:cover;
    }
</style>

我尝试了一些代码,包括

<div id="one">
<a id="link1" href="https://click.com" target="_top"></a>
</div>

<div id="two">
<a id="link2" href="https://click2.com" target="_top"></a>
</div>

Class of ID Change based on URL - URL Based Image Swap - Creating Conditional Statement Based On Page URL ETC

1 个答案:

答案 0 :(得分:1)

根据胡安(Juan)的评论,两个车身标签并不是可行的方法。 https://developer.mozilla.org/en-US/docs/Web/HTML/Element#Sectioning_root。您应该改用父包装器。

<div class="parent">
<a id="link2" href="https://click.com" target="_top"></a>
</div>

使用window.location将帮助您解析当前URL,以便您可以正确执行所需的操作。

我要做的是查询具有window.location.href的href属性的元素,然后获取.parentElement以获得直接父级。

const url = window.location.href;
const targetParent = document.querySelector('.parent a[href="' + url + '"]').parentElement;

targetBody.style.display = "block";

如果<a>位于更深的地方,则此操作将无效。既然如此,我会说为父级添加另一个属性,以便我们直接查询它。

<div class="parent" url="https://click.com">
<a id="link2" href="https://click.com" target="_top"></a>
</div>

然后

const url = window.location.href;
const targetParent = document.querySelector('.parent[url="' + url + '"]').parentElement;

如果您的网址还有其他多余的字符串,例如http://click.com?id=foobar,那么这也将不起作用。您将必须手动连接window.location中的其他属性,以便只能获得所需的内容。

const baseDomainWithSubPath = window.location.origin + "/" + window.location.pathname;
const baseDomain = window.location.origin;
const subDomain = window.location.pathname;