在网站pib.nic.in/newsite/erelease.aspx中,左侧链接在同一页面中打开 - 这在阅读和切换到下一个主题时会带来不便。
一个名为Getrelease
的函数正在传递与所点击的项目相对应的唯一ID,以便在右侧列上进行提取和显示。
相关网页,HTML代码段为:
<div class="leftrightdiv" id="lreleaseID">
<ul class="link1">
<li style="list-style:none;border-bottom: blue 1px dotted;color:blue;font-size:110%" class="rel">Prime Minister's Office</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" **onclick='Getrelease(94318)'** >PM's statement at the BRICS Leaders - Africa Dialogue Forum<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="Getrelease(94318)" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">PM's statement at the BRICS Leaders - Africa Dialogue Forum</li>
<li style="list-style:none;border-bottom: blue 1px dotted;color:blue;font-size:110%" class="rel">Ministry of Finance</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94339)' >Central Board of Excise and Customs (CBEC) Offices to Remain open on 29th, 30th & 31stmarch, 2013 ;<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="Getrelease(94339)" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">Central Board of Excise and Customs (CBEC) Offices to Remain open on 29th, 30th & 31stmarch, 2013 ;</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94338)' >India and Liechtenstein Sign Tax Information Exchange Agreement (TIEA)<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="Getrelease(94338)" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">India and Liechtenstein Sign Tax Information Exchange Agreement (TIEA)</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94337)' >Government Committed to bring down the Current Account<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="Getrelease(94337)" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">Government Committed to bring down the Current Account</li>
</ul>
<br>
</div>
我想将上面的代码更改为以下代码,以便在单击该项目时,它将在新页面中打开。
<div class="leftrightdiv" id="lreleaseID">
<ul class="link1">
<li style="list-style:none;border-bottom: blue 1px dotted;color:blue;font-size:110%" class="rel">Prime Minister's Office</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94318)' >PM's statement at the BRICS Leaders - Africa Dialogue Forum<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="window.open('http://www.pib.nic.in/newsite/erelease.aspx?relid=94318')" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">PM's statement at the BRICS Leaders - Africa Dialogue Forum</li>
<li style="list-style:none;border-bottom: blue 1px dotted;color:blue;font-size:110%" class="rel">Ministry of Finance</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94339)' >Central Board of Excise and Customs (CBEC) Offices to Remain open on 29th, 30th & 31stmarch, 2013 ;<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="window.open('http://www.pib.nic.in/newsite/erelease.aspx?relid=94339')" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">Central Board of Excise and Customs (CBEC) Offices to Remain open on 29th, 30th & 31stmarch, 2013 ;</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94338)' >India and Liechtenstein Sign Tax Information Exchange Agreement (TIEA)<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="window.open('http://www.pib.nic.in/newsite/erelease.aspx?relid=94338')" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">India and Liechtenstein Sign Tax Information Exchange Agreement (TIEA)</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94337)' >Government Committed to bring down the Current Account<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="window.open('http://www.pib.nic.in/newsite/erelease.aspx?relid=94337')" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">Government Committed to bring down the Current Account</li>
</ul>
<br>
</div>
所以写了这个Greasemonkey脚本:
// ==UserScript==
// @name Press Information Bureau
// @namespace http://userscripts.org/scripts/show/163329
// @description PIB new tab open
// @include http://pib.nic.in*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// @version 2.4
// ==/UserScript==
function replaceOnclick() {
var list = document.getElementById('lreleaseID');
var lists = list.getelementsbytagname('li');
for (i = 0; i < lists.length; i++) {
var Str = lists[i].getAttribute("onclick");
var seconds = Str.indexOf(')');
var url = "http://www.pib.nic.in/newsite/erelease.aspx?relid=" + Str.substring(11, second);
lists[i].setAttribute("onclick", "window.open('" + url + "')");
}
}
Greasemonkey脚本正在注入。但它没有对产出做出预期的改变
javascript使用中的错误是什么?请帮助我。
答案 0 :(得分:1)
该脚本有多处错误:
它定义replaceOnclick()
但从不调用它。 这就是您在错误控制台中看不到任何操作且没有错误的原因( Ctrl Shift J )。
无效的函数名称getelementsbytagname
将抛出错误(异常)。 JavaScript区分大小写,正确的函数是getElementsByTagName()
。
Str
有时为null,因此var seconds=Str.indexOf(')');
会抛出异常并导致脚本崩溃。防止这种情况的一种方法是将其用于if (Str) {...}
语句。
同样,seconds
有时为空。
这一行:
var url="http://www.pib.nic.in/newsite/erelease.aspx?relid="+Str.substring(11, second);
使用错误的名称。 second
应为seconds
。
还有一些不完全错误的项目,但你应该做的不同......
该网站默认为网址http://www.pib.nic.in/newsite/...
。因此,您可能希望添加一个包含行来解释:
// @include http://www.pib.nic.in*
该脚本引用jQuery,但不使用它!使用jQuery,它将为您节省时间和悲伤。
脚本未指定a @grant
directive。这可能会导致意外的行为和冲突。如果可以避免,请始终指定至少一个@grant
行,不要使用@grant none
。
jQuery 1.3.2是“Hella”已过时。如果您打算使用jQuery,请使用更新版本,除非有引人注目的理由不这样做。
使用上面的项目来修复该脚本。或者这是使用jQuery的样子:
// ==UserScript==
// @name Press Information Bureau
// @namespace http://userscripts.org/scripts/show/163329
// @description PIB new tab open
// @include http://pib.nic.in*
// @include http://www.pib.nic.in*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant GM_addStyle
// @version 2.4
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
//-- Use jQuery selector to get just the <li>s that have an onclick.
var articleLinks = $("#lreleaseID li[onclick]");
//-- jQuery .each()
articleLinks.each ( function () {
var jThis = $(this); // "this" is a special var inside .each()
var onClickVal = jThis.attr ("onclick");
var articleMatch = onClickVal.match (/Getrelease\s*\((\d+)\)/i);
if (articleMatch && articleMatch.length == 2) {
var articleId = articleMatch[1];
var articleUrl = "http://www.pib.nic.in/newsite/erelease.aspx?relid="
+ articleId
;
jThis.attr ("onclick", "window.open('" + articleUrl + "')");
}
} );