我試圖找到一個包含圖像的節點,然後在該圖像的標記中找到特定的字符串。但我不知道在頁面上何時會出現圖像標籤,除了它僅出現在分類爲bodyTextSmall
的td
元素中。這應該讓我在那裏,對吧?無法獲取childnodes.length
一切正常,直到我試圖獲得childNodes
屬性的length
。看到上面的行。
這是正確的代碼,還是有另一種方法來確定某個特定的td
標記中是否有img
標記?我正在撕掉我留下的小小的頭髮,因爲從我對JavaScript的瞭解不多,這就是它應該如何工作。
的問題是,我只用img
標籤知道我是否需要格式化td
標籤,我不組裝td
標籤和不能進入該網站直接進行修改的那部分。我已經嘗試過使用CSS選擇器來查看是否可以對圖像的某些部分進行樣式設置,但是有內聯樣式,我無法重寫。同樣,這是我無法控制的網站的一部分。
<script type="text/javascript">
function getTables(dRef) {
var d = document;
var dMid = d.getElementById('mainContent');
var dTabs = dMid.getElementsByTagName('td');
// Attempts to load table cells...
for (var i = 0; i < dTabs.length; i++) {
// Attempts to detect the specific cell type...
if (dTabs[i].className == "bodyTextSmall") {
// Attempts to locate the parts inside this cell...
var myNodes = i.ChildNodes; // This part runs, but I can't get "ChildNodes.length" to work
// Attempts to notify the user...
alert(dTabs[i].className + ': ' + i);
}
}
}
window.onload = getTables;
</script>
所以,現在我已經切換到jQuery,我也遇到了同樣的問題。我可以告訴哪個圖像在哪裏,但我無法對圖像本身做任何事情。
<script type="text/javascript">
function getTables() {
// Locates the correct container for images...
$('#mainContent img').each(function(idx, item) {
var tImgs = item.src;
if (tImgs.indexOf("-ds.") > 0) {
window.alert(tImgs);
$(this).append('Here is a message about the image.'); //nothing happens here
}
});
}
window.onload = getTables;
</script>
同樣,這個jQuery代碼響應任何具有「-ds」的圖像。在src的任何地方。我無法得到任何其他事情發生。我知道$(img).append會影響每個圖片,所以腳本可以做某些事情。
這現在已經成爲一種比我想要的還要多的東西,而且它真的開始讓我最後的神經感到厭倦。
+1我錯過了 – SLaks 2011-12-19 18:53:00