Javascript 如何使(Java)脚本获取URL并基于该URL执行if语句(#transclusion,#highlight)
链接到html文档中的特定部分(带有特定的hashtag/id)很容易。突出目标部分比我想象的要复杂。任何帮助都将不胜感激 我想在表面上做的是。。。允许用户单击一个链接,该链接将用户带到另一个html文档的特定部分(可以是div、p、span),该部分在该点高亮显示(更改文本bg颜色) 我是这样做的。我试图编写一个脚本,该脚本将: 首先,获取文档的URL并将其设置为变量; 其次,脚本将识别URL(var)的末尾是否有一个hashtag,即用户是否链接到URL的特定部分; 第三,如果是这种情况,脚本将更改所讨论部分的背景颜色 如果我犯了很多新手错误,我真诚地道歉。我在onload和onclick中得到了突出显示,但不是在url中有标签的条件下。这将使我能够通过从URL中删除#xyz来删除突出显示 以下是脚本:Javascript 如何使(Java)脚本获取URL并基于该URL执行if语句(#transclusion,#highlight),javascript,html,css,Javascript,Html,Css,链接到html文档中的特定部分(带有特定的hashtag/id)很容易。突出目标部分比我想象的要复杂。任何帮助都将不胜感激 我想在表面上做的是。。。允许用户单击一个链接,该链接将用户带到另一个html文档的特定部分(可以是div、p、span),该部分在该点高亮显示(更改文本bg颜色) 我是这样做的。我试图编写一个脚本,该脚本将: 首先,获取文档的URL并将其设置为变量; 其次,脚本将识别URL(var)的末尾是否有一个hashtag,即用户是否链接到URL的特定部分; 第三,如果是这种情况,脚
<script type="text/javascript">
var highlight = function () {
var url = window.location.href;
if (url === "http://whatever.com/highlight.html#link") {
document.getElementById('link').style.background = '#FA5858';
} else {
//do nothing
};
window.onload = highlight;
</script>
var highlight=函数(){
var url=window.location.href;
如果(url==”http://whatever.com/highlight.html#link") {
document.getElementById('link').style.background='#FA5858';
}否则{
//无所事事
};
window.onload=高亮显示;
以下是整个HTML:
<!DOCTYPE="html">
<head>
<title>highlight</title>
<style type="text/css">
.textmain {
margin-right: auto;
margin-left: auto;
width: 500;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="jquery.inview.js"></script>
<script type="text/javascript">
var highlight = function () {
var url = window.location.href;
if (url === "http://pleasepick.co.nf/highlight.html#link") {
document.getElementById('link').style.background = '#FA5858';
} else {
//do nothing
};
window.onload = highlight;
</script>
</head>
<body>
<div class="textmain">
<p>Chinese website Xianghuo.com reported a recent incident involving a Hong Kong resident whose Samsung Galaxy S4 allegedly exploded and caught fire while he was playing “Love Machine,” one of the most popular games for Android.</p>
<p>After hearing a loud popping sound, Mr. Du threw his smartphone onto the couch, where flames quickly spread and caused substantial damage to the house. He and his wife managed to escape unscathed.</p>
<p>Another similar case was reported a month before this, in which an 18-year-old Swiss girl suffered third-degree burns on her leg from an exploding Samsung Galaxy S3 placed in her pocket. In 2012, the same case was reported in Dublin, where a man’s Galaxy S3 caught fire on his car’s dashboard.</p>
<p>Another incident involved Apple, Samsung’s key competitor. Reports state that a Chinese woman died from an electric shock when she answered a call on her iPhone 5 while the phone was charging. Another man reportedly suffered the same fate, only this time it led to a coma.</p>
<p>For Apple, using official chargers is a must for avoiding damage to the phone, not to mention physical injuries.
In a number of cases, unofficial third-party products were pointed out as the cause of the smartphone mishaps. As in the case of the Swiss woman, her phone was outfitted with a discounted replacement battery. But in Mr. Du’s case, he claimed to have used all legitimate Samsung products.</p>
<p>Samsung’s Hong Kong unit is currently investigating the incident.</p>
<br>
<p>Chinese website Xianghuo.com reported a recent incident involving a Hong Kong resident whose Samsung Galaxy S4 allegedly exploded and caught fire while he was playing “Love Machine,” one of the most popular games for Android.</p>
<p>After hearing a loud popping sound, Mr. Du threw his smartphone onto the couch, where flames quickly spread and caused substantial damage to the house. He and his wife managed to escape unscathed.</p>
<p>Another similar case was reported a month before this, in which an 18-year-old Swiss girl suffered third-degree burns on her leg from an exploding Samsung Galaxy S3 placed in her pocket. In 2012, the same case was reported in Dublin, where a man’s Galaxy S3 caught fire on his car’s dashboard.</p>
<p id="link">Another incident involved Apple, Samsung’s key competitor. Reports state that a Chinese woman died from an electric shock when she answered a call on her iPhone 5 while the phone was charging. Another man reportedly suffered the same fate, only this time it led to a coma.</p>
<p>For Apple, using official chargers is a must for avoiding damage to the phone, not to mention physical injuries.
In a number of cases, unofficial third-party products were pointed out as the cause of the smartphone mishaps. As in the case of the Swiss woman, her phone was outfitted with a discounted replacement battery. But in Mr. Du’s case, he claimed to have used all legitimate Samsung products.</p>
<p>Samsung’s Hong Kong unit is currently investigating the incident.</p>
</div>
</body>
突出
.textmain{
右边距:自动;
左边距:自动;
宽度:500;
}
var highlight=函数(){
var url=window.location.href;
如果(url==”http://pleasepick.co.nf/highlight.html#link") {
document.getElementById('link').style.background='#FA5858';
}否则{
//无所事事
};
window.onload=高亮显示;
中国网站香火网报道了最近一宗涉及香港居民的事件,三星Galaxy S4在玩“爱情机器”时被炸毁并起火,这是Android最受欢迎的游戏之一。
听到巨大的爆裂声后,杜先生把他的智能手机扔到沙发上,火势迅速蔓延,造成房屋严重损坏。他和妻子设法毫发无损地逃脱
一个月前,另一起类似的案例被报道,一名18岁的瑞士女孩因口袋里的三星Galaxy S3爆炸而腿部三度烧伤。2012年,都柏林也发生了同样的案例,一名男子的Galaxy S3汽车仪表盘起火
另一起事件涉及三星的主要竞争对手苹果。报道称,一名中国妇女在iPhone 5充电时接听电话时触电身亡。另一名男子据称也遭遇了同样的命运,只是这次导致昏迷
对苹果来说,使用官方充电器是避免手机损坏的必要条件,更不用说身体伤害了。
在许多案例中,非官方的第三方产品被指出是智能手机事故的原因。就像这名瑞士妇女的案例一样,她的手机配备了打折的备用电池。但在杜先生的案例中,他声称使用了所有合法的三星产品
三星香港公司目前正在调查这一事件。
中国网站香火网报道了最近一宗涉及香港居民的事件,三星Galaxy S4在玩“爱情机器”时被炸毁并起火,这是Android最受欢迎的游戏之一。
听到巨大的爆裂声后,杜先生把他的智能手机扔到沙发上,火势迅速蔓延,造成房屋严重损坏。他和妻子设法毫发无损地逃脱
一个月前,另一起类似的案例被报道,一名18岁的瑞士女孩因口袋里的三星Galaxy S3爆炸而腿部三度烧伤。2012年,都柏林也发生了同样的案例,一名男子的Galaxy S3汽车仪表盘起火
另一起事件涉及三星的主要竞争对手苹果。报道称,一名中国妇女在iPhone 5充电时接听电话时触电身亡。另一名男子据称也遭遇了同样的命运,只是这次导致昏迷
对苹果来说,使用官方充电器是避免手机损坏的必要条件,更不用说身体伤害了。
在许多案例中,非官方的第三方产品被指出是智能手机事故的原因。就像这名瑞士妇女的案例一样,她的手机配备了打折的备用电池。但在杜先生的案例中,他声称使用了所有合法的三星产品
三星香港公司目前正在调查这一事件。
如果要检测url中的#标记,请尝试此操作
var highlight = function () {
var url = window.location.href,
HashTag=/#/g,
checkHashTag=url.test(HashTag);
if (checkHashTag) {
document.getElementById('link').style.background = '#FA5858';
} else {
//do nothing
};
window.onload = highlight;
也许你可以试试:
if (url.match(/#link/) == "#link") {
document.getElementById('link').style.background = '#FA5858';
} else {
//do nothing
}
并删除“;”after-else{//do nothing}语句:-)使用而不是
window.location.href
。是否尝试检测URL中的#标记Yes i am…..blablbab(字符min)嗯…有window.location.hash
,所以测试window.location.hash.length>0
可能就不那么复杂了。如果你使用window.location.hash,你不仅会选择#,而且会选择指向它的任何东西,例如,使用window.location.hash,你会选择#link not only#。在Treakec的情况下,我觉得它似乎不是他只需要选择任何带有或包含hastag“#”的url,我尝试了这两种方法。都不起作用。我想window.location.hash是一种方法,因为我以后可以链接到文档中的多个标记/突出显示。修复了它