链接到html文档内的特定部分(具有特定的hashtag/id)是容易的。突出显示目标部分比我想象的要复杂得多。任何帮助都将不胜感激。
我试着在表面上做的是...使得用户能够点击链接,该链接将他带到在该点被突出显示(改变文本bg颜色)的另一html文档的特定部分(可以是div、p、span)。
我尝试编写一个脚本,它将:首先,获取文档的URL并将其设置为变量;其次,脚本将识别URL (var)是否在末尾具有hashtag,即,如果用户链接到URL的特定部分;第三,如果是这样,脚本将更改相关部分的背景颜色。
如果我犯了很多新手的错误,我真诚地道歉。我让高亮显示在onload和onclick上工作,但不是在url中有一个标签的情况下。这将使我能够通过从URL中删除#xyz来删除突出显示。
脚本如下:
<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>下面是整个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>发布于 2013-09-26 07:14:39
如果要检测url中的#tag,请尝试此操作。
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;发布于 2013-09-26 10:34:39
也许你可以试试:
if (url.match(/#link/) == "#link") {
document.getElementById('link').style.background = '#FA5858';
} else {
//do nothing
}并在else{ //do nothing}语句后面删除";“:-)
https://stackoverflow.com/questions/19016563
复制相似问题