简体   繁体   English

在JavaScript中获取表tr td中传递的参数

[英]get the passed argument in table tr td in javascript

I have created a table structure in which each td has a text. 我创建了一个表结构,其中每个td都有一个文本。 calling an onclick function changeFocus(liVSE) doesnt return me liVSE, while alerting it, displays as [object TableCellElement].. What should I do if I need to alert it as (liVSE). 调用onclick函数changeFocus(liVSE)不会向我返回liVSE,同时提醒它,显示为[object TableCellElement]。如果我需要将其警告为(liVSE),该怎么办。 Or please suggest any other easier method to get the idname 或者,请提出其他更简单的方法来获取idname

Please see me as newbie HTML CSS Javascript developer. 请视我为新手HTML CSS Javascript开发人员。

function changeFocus(idname){
    alert(idname);
    clearActive();
    //"info-"+idname.style.display = "block";
    idname.className = "tab-active";
    return true;
}

<table>
    <tbody>
        <tr>
            <td id="liVSE" class="tab-active"  ><span class="tabcorner"><span></span></span><a href="#info-liVSE" onclick="changeFocus(liVSE)">li VSE</a></td>
            <td id="liGoals" class="tab"><span class="tabcorner"><span></span></span><a href="#info-liGoals" onclick="changeFocus(liGoals)">li Goals</a></td>
            <td id="CisoInitiatives" class="tab"><span class="tabcorner"><span></span></span><a href="#info-CisoInitiatives" onclick="changeFocus(CisoInitiatives)">li Iniatives</a></td>
            <td id="EntBusCounsilVSE" class="tab"><span class="tabcorner"><span></span></span><a href="#info-EntBusCounsilVSE" onclick="changeFocus(EntBusCounsilVSE)">Ent. Bus. Council VSE</a></td>
            <td id="DSSGVSE" class="tab"><span class="tabcorner"><span></span></span><a href="#info-DSSGVSE"onclick="changeFocus(DSSGVSE)"> VSE</a></td>
            <td id="SSPGVSE" class="tab"><span class="tabcorner"><span></span></span><a href="#info-SSPGVSE" onclick="changeFocus(SSPGVSE)"> VSE</a></td>
            <td id="SSPGGoals" class="tab"><span class="tabcorner"><span></span></span><a href="#info-SSPGGoals" onclick="changeFocus(SSPGGoals)"> Goals</a></td>
            <td id="CPDMEffectivity" class="tab"><span class="tabcorner"><span></span></span><a href="#info-Effectivity" onclick="changeFocus(Effectivity)"> Effectivity</a></td>
        </tr>
    </tbody>
</table>

To avoid headaches in the future, i suggest you separate your html from your script (the onclick attribute is a thing of the past, really). 为了避免将来出现麻烦,我建议您将html与脚本分开( onclick属性已经成为过去了)。 You could do something like the following from an external javascript file (linked at the end of the document) 您可以从外部javascript文件(在文档末尾链接)执行以下操作

function clickHandler(e) {
    // e = event object
    // this = <a>

    e.preventDefault(); // prevent following the link (optional)

    alert(this.parentNode.id); // alert the id of the parent node

    // check your js console for what other properties you get available from the <a>
    console.dir(this);

    // uncomment to alert the value of the href attribute, for instance
    //alert(this.attributes.href.value);

}

var links = document.getElementsByTagName("a");
var i = links.length;

// loop through the links
while (i--) {
    // attach the click handler to each <a>
    links[i].addEventListener("click", clickHandler);
}

Have a look at http://jsfiddle.net/Tv3Lx/2/ where i removed the inline script calls ( onclick attribute) 看看http://jsfiddle.net/Tv3Lx/2/ ,其中我删除了内联脚本调用( onclick属性)

There are plenty of things wrong with this code (you likely don't want to select all the elements on the page, old IE uses "attachEvent" instead of "addEventListener") but hopefully get you started in the right direction. 这段代码有很多问题(您可能不想选择页面上的所有元素,旧版IE使用“ attachEvent”而不是“ addEventListener”),但希望您可以朝正确的方向入手。 MDN is a great resource on JavaScript, with plenty examples MDN是JavaScript的绝佳资源,其中有很多示例

try changing onclick="changeFocus(liVSE)" 尝试更改onclick="changeFocus(liVSE)"

to

onclick="changeFocus('liVSE')"

Note: the Quotes ' around liVSE 注意:行情'周围liVSE

change onclick event for all the element to 将所有元素的onclick事件更改为

changeFocus(this.parentNode.id);

Should work for you 应该为你工作

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

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