简体   繁体   English

如何使用JavaScript访问中继器内的Div

[英]How to access a Div inside a repeater using javascript

Simply, I have an anchor inside a repeater that triggers on click a javascript function, which sets the innerHTML of a Div to some content. 简而言之,我在转发器中有一个锚点,该锚点在单击时触发一个javascript函数,该函数将Div的innerHTML设置为某些内容。

trying this outside a repeater did work! 在中继器之外尝试此功能确实有效! but if I tried to implement the same code in the repeater's item template, it won't work! 但是,如果我尝试在中继器的项目模板中实现相同的代码,它将无法正常工作!

NOTE: I think I need to access the repeater first then access the Anchor inside it! 注意:我想我需要先访问中继器,然后访问其中的锚点! but I don't know how to do it 但我不知道该怎么做

For further illustration: 进一步说明:

JavaScript Function: JavaScript函数:

function show(ele, content) {
    var srcElement = document.getElementById(ele);
    if (srcElement != null) {
        srcElement.innerHTML = content;
    }
}

The repeater's code: 中继器的代码:

<asp:Repeater ID="Repeater1" runat="server" >
    <ItemTemplate>
        Name : <%# Eval("name")%>
        <DIV ID= "PersonalInfo1" runat="server"></DIV>
        <A id="A1" href="#" runat="server" onclick="show('PersonalInfo1','Address : ')">More...</A>
    </ItemTemplate>
</asp:Repeater>

PS: THE POSTED CODE ISN'T WORKING IN THE REPEATER! 附言:所发布的代码无法在中继器中使用!

That is because id's are unique. 那是因为id是唯一的。 Select elements using getElementsByName or by their class name with for example jQuery. 使用getElementsByName或按其类名(例如jQuery)选择元素。

OK... let's start over. 好...让我们重新开始。

Have such repeater code: 有这样的中继器代码:

<asp:Repeater ID="Repeater1" runat="server" >
    <ItemTemplate>
        <div>
            Name : <%# Eval("name")%>
            <div id="Address" runat="server" style="display: none;"><%# Eval("address")%></div>
            <div id="Interests" runat="server" style="display: none;"><%# Eval("interests")%></div>
            <a id="A1" href="#" runat="server" onclick="return show(this, 'Address');">Show address</a>
            <a id="A2" href="#" runat="server" onclick="return show(this, 'Interests');">Show interests</a>
        </div>
    </ItemTemplate>
</asp:Repeater>

Then such JavaScript code: 然后是这样的JavaScript代码:

function show(oLink, targetDivID) {
    var arrDIVs = oLink.parentNode.getElementsByTagName("div");
    for (var i = 0; i < arrDIVs.length; i++) {
        var oCurDiv = arrDIVs[i];
        if (oCurDiv.id.indexOf(targetDivID) >= 0) {
            var blnHidden = (oCurDiv.style.display == "none");
            oCurDiv.style.display = (blnHidden) ? "block" : "none";
            //oLink.innerHTML = (blnHidden) ? "Less..." : "More...";
        }
    }
    return false;
}

This will search for "brother" DIV element of the clicked link, and show or hide it. 这将搜索单击的链接的“兄弟” DIV元素,并显示或隐藏它。

The code is as simple as possible using pure JavaScript, you should be able to understand what each line is doing - feel free to ask if you don't. 使用纯JavaScript的代码尽可能简单,您应该能够理解每一行的内容-随时询问是否没有。 :) :)

Note, you have to put the personal info in the PersonalInfo div in advance instead of passing it to the function - the function will get pointer to the clicked link. 请注意,您必须事先将个人信息放入PersonalInfo div中,而不是将其传递给函数-该函数将获得指向所单击链接的指针。

Yes you need to iterate all the relevant links. 是的,您需要迭代所有相关链接。 Solution that involve minimal change of code is adding class to the links then check for this class: 涉及最少代码更改的解决方案是将类添加到链接中,然后检查该类:

<A id="A1" href="#" runat="server" class="RepeaterLink" ...>

And then in JavaScript: 然后在JavaScript中:

var arrLinks = document.getElementsByTagName("a");
for (var i = 0; i < arrLinks.length; i++) {
    var oLink = arrLinks[i];
    if (oLink.className == "RepeaterLink") {
        //found link inside repeater..
        oLink.click();
    }
}

This will "auto click" all the links, you can check ID or something else to imitate click of specific link in the repeater as well. 这将“自动单击”所有链接,您也可以检查ID或其他内容来模仿转发器中特定链接的单击。

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

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