简体   繁体   English

使用子节点innerHTML更改元素的标题

[英]Changing the title of an element using a child nodes innerHTML

I've got a webpage which uses the JQuery ToolTips plugin for popup boxes with extra information. 我有一个网页,该网页使用JQuery ToolTips插件来显示带有额外信息的弹出框。 That's all working fine with no issues. 一切正常,没有问题。

However in order for the popup boxes to work perfectly I need to give the elements in question titles detailing out what I want the popups to say. 但是,为了使弹出框正常工作,我需要在问题标题中提供要详细说明我要弹出框说的内容的元素。 This would be simple if I could change the HTML directly but I can only do it through JS. 如果我可以直接更改HTML,但只能通过JS进行操作,这将很简单。 More to bother me, the tooltip changes depending on the content of what's in the elements innerHTML, but the element itself doesn't have an ID. 更让我困扰的是,工具提示会根据元素innerHTML中内容的内容而变化,但元素本身没有ID。 It's parent however does have an ID. 但是它的父母确实有一个ID。

so I've been trying to access the child node through the parent, reads it's innerHTML, compare it to a few if statements and then apply a title to the parent based on this innerHTML. 因此,我一直在尝试通过父级访问子节点,读取它的innerHTML,将其与一些if语句进行比较,然后基于此innerHTML将标题应用于父级。

Here is my code: 这是我的代码:

for(var i = 1; i<5; i++){
        var q = document.getElementById("cell.0."+i);
        console.log(i+" "+q);
        if(q.children[0].innerHtml === "some text1"){
            q.setAttribute('title', 'Other text1');
        }
        else if(q.children[0].innerHtml === "some text2"){
            q.setAttribute('title', 'Other text2');
        }
        else if(q.children[0].innerHtml === "some text3"){
            q.setAttribute('title', 'Other text3');
        }
        else if(q.children[0].innerHtml === "some text4"){
            q.setAttribute('title', 'Other text4');
        }
    }

And an accompanying JSFiddle to make it a bit clearer: http://jsfiddle.net/qxb58/7/ 以及随附的JSFiddle,使其更加清晰: http : //jsfiddle.net/qxb58/7/

Note: the JSFiddle uses a button, but the actual function I'm using will be on page load. 注意:JSFiddle使用一个按钮,但是我正在使用的实际功能将在页面加载中。

I've been testing each line in the console. 我一直在测试控制台中的每一行。 From what I can tell all of the statements which are immediately testable in the console work (egqchildren[0].innerHtml === "some text4" and q.setAttribute('title', 'Other text4');). 据我所知,所有可以在控制台工作中立即测试的语句(例如,egqchildren [0] .innerHtml ===“ some text4”和q.setAttribute('title','Other text4');)。 But when put into a for loop it doesn't seem to work. 但是,当放入for循环时,它似乎不起作用。

Note it's unlikely to be a HTML error as my HTML is autogenerated and works. 请注意,由于我的HTML是自动生成的并且可以正常工作,因此不太可能是HTML错误。 If there is an error with the HTML in the fiddle, it's me being crap at HTML. 如果小提琴中的HTML出现错误,那是我被HTML废了。 Thanks for pointing it out though! 感谢您指出这一点!

EDIT: Solution: innerHTML not innerHtml. 编辑:解决方案:innerHTML而不是innerHtml。 Sorry for the stupid mistake. 很抱歉这个愚蠢的错误。 And thanks for the help! 并感谢您的帮助!

In HTML ids are case sensitive. 在HTML中,ID区分大小写。 You set them in lower case and query them in JS with an upper case C . 您将它们设置为小写,然后在JS中使用大写C

Correct it this way: 通过以下方式更正它:

q = document.getElementById("cell.0."+i);
                             ^ lower case now

This will not solve the problem but is just one of a multi-step debug. 这不会解决问题,而只是多步骤调试之一。 Below is a further answer solving the problem. 以下是解决问题的进一步解答。

EDIT: this works now. 编辑:现在可以使用。 I corrected the upper case C and replaced innerHtml with innerText . 我更正了大写C并用innerText替换了innerHtml

And BTW, innerHtml is written this way: 顺便说一句, innerHtml是这样写的:

innerHTML
     ^^^^ all upper case

Copy & paste solution: 复制和粘贴解决方案:

function changer()
{
    for (var i = 1; i < 5; i++)
    {
        var q = document.getElementById("cell.0." + i);

        if (q.children[0].innerText === "some text1") {
            q.setAttribute('title', 'Other text1');
        } else if (q.children[0].innerText === "some text2") {
            q.setAttribute('title', 'Other text2');
        } else if (q.children[0].innerText === "some text3") {
            q.setAttribute('title', 'Other text3');
        } else if (q.children[0].innerText === "some text4") {
            q.setAttribute('title', 'Other text4');
        }
    }
}

Aside from your case problem, you also have the problem of your HTML being invalid. 除了大小写问题之外,您还遇到HTML无效的问题。 Enclose your <tr> s in a and it works: <tr>括在a中,即可:

http://jsfiddle.net/qxb58/7/ http://jsfiddle.net/qxb58/7/

<table>
    <tr>
        <td id="cell.0.1"> <span>some text1</span>

        </td>
    </tr>doc
    <br/>
    <tr>
        <td id="cell.0.2"> <span>some text2</span>

        </td>
    </tr>
    <br/>
    <tr>
        <td id="cell.0.3"> <span>some text3</span>

        </td>
    </tr>
    <br/>
    <tr>
        <td id="cell.0.4"> <span>some text4</span>

        </td>
    </tr>
</table>
<br/>
<br/>
<button onClick="changer()">Changing</button>

Still not 100% correct, but working. 仍然不是100%正确,但可以正常工作。

Oh, also, you need to set your fiddle to put your script in the head. 哦,同样,您需要摆弄小提琴才能将脚本放在首位。 You had it in the onload which won't work because the handler won't be defined. 您将其放在onload中将无法使用,因为不会定义处理程序。

Edit: Sorry, missed the part of actually setting the title. 编辑:对不起,错过了实际设置标题的部分。 Your problem there is that you need innerText not innerHTML . 你的问题是你需要innerText而不是innerHTML See here: http://jsfiddle.net/qxb58/15/ 看到这里: http : //jsfiddle.net/qxb58/15/

Finally: if you are using a jQuery plugin for your tooltips, why not use jQuery throughout instead of getElementById 最后:如果您使用jQuery插件作为工具提示,为什么不使用jQuery而不是getElementById

You were executing javascript onload which meant that when the DOM was created with the function changer() , it was undfined at that time. 您正在执行javascript onload,这意味着当使用功能changer()创建DOM时,它当时是未定义的。 Also you were looking for property innerHtml instead of innerHTML . 另外,您在寻找属性innerHtml而不是innerHTML Properties are case senstive. 属性是区分大小写的。 With these two changes, it works fine: 通过这两个更改,它可以正常工作:

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM