Javascript通过元素替换表的单个部分
长话短说,我在写一个油腔滑调的剧本。我处理的页面有一个特定的表,根据页面的不同,表中有不同的值Javascript通过元素替换表的单个部分,javascript,html,html-table,greasemonkey,userscripts,Javascript,Html,Html Table,Greasemonkey,Userscripts,长话短说,我在写一个油腔滑调的剧本。我处理的页面有一个特定的表,根据页面的不同,表中有不同的值 <table class="billing dead"> <tbody> <tr> <th>Index:</th> <td>Long alpha numeric value here</td> </tr>
<table class="billing dead">
<tbody>
<tr>
<th>Index:</th>
<td>Long alpha numeric value here</td>
</tr>
<tr>
<th>Status:</th>
<td class="status">This text is red to denote urgency.</td>
</tr>
</tbody>
</table><!-- end billing dead -->
这取代了整个表格,但它是最接近我的情况的问题/答案;只是不太对。一旦我找到了具有上面链接的方法的表,是否有方法访问该特定表的元素?我试过随意改变
elems[i].innerHTML = content;
进入
这很快教会了我,
elems[i]
不是节点的节点。有没有一个简单的方法来做我想做的事 效果很好。这是最新的
replaceContentInContainer('status','hello')代码>是否有多个“账单”表?是否每个行只有一个“索引”,并且总是第一行
以下,complete script显示了如何更改任何/所有“索引”行的HTML,包括激活生成的按钮。它使用,这将节省您很多时间和精力
/==UserScript==
//@name\u按钮化索引单元格
//@包括http://YOUR_SERVER.COM/YOUR_PATH/*
//@需要http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
//==/UserScript==
//---查找所有“索引”标题。
var indexHdrs=$(“table.billing.dead th:contains(Index)”);
//---例如,将它们全部包装在一个按钮中。
indexHdrs.each(函数(){
$(this.wrapInner(“”);
} );
/*---启动按钮。在这种情况下,它们只显示匹配项
单元格内容。
*/
$(“table.billing.th button.myButton”)。单击(函数(){
var jThis=$(this);//--单击的按钮。
var matchingCell=jThis.parent().next();
警报(matchingCell.text());
} );
注意:若要在Chrome上使用此脚本,请安装--您无论如何都要这样做,既可以增加额外功能,也可以克服Chrome对userscript安装的未决限制。我们是否可以向包含“Index”的th
元素添加一个类,并使用该类而不是表的类?您不使用jQuery有什么原因吗?@JimmyX:不幸的是,没有。这不是我生成的网站,而是一个用户脚本脚本,用于处理我经常使用的网站。@RalphLavelle:I=Noob。这就是为什么。jQuery可以在chrome/firefox跨平台中作为用户脚本使用吗?如果是的话,这个问题的最佳解决方案是什么?如果您可以绝对确定所讨论的th
始终是表中的第一个th
,请查看@am not i am的答案。我们没有将整个jQuery库嵌入到一行简单的javascript中,不幸的是,我正在尝试更改“Index:”它没有一个类直接链接到它。谢谢你编辑:另外,这个网站是我将来要使用的,非常感谢!非常感谢你。这实际上回答了我的问题,也解决了我正在寻找的下一步;把它变成一个按钮。非常感谢你!
elems[i].innerHTML = content;
elems[i][0].innerHTML = content;
document.querySelector('.billing.dead > tbody > tr > th')
.firstChild
.data = "new value";
// ==UserScript==
// @name _Buttonize the Index cells
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// ==/UserScript==
//--- Find all the "Index" headers.
var indexHdrs = $("table.billing.dead th:contains(Index)");
//--- Wrap them all in a button, for example.
indexHdrs.each ( function () {
$(this).wrapInner ('<button class="myButton" />');
} );
/*--- Activate the buttons. In this case they just display matching
cell contents.
*/
$("table.billing.dead th button.myButton").click ( function () {
var jThis = $(this); //-- The button that was clicked.
var matchingCell = jThis.parent ().next ();
alert (matchingCell.text () );
} );