简体   繁体   English

如何使我的JavaScript在转发器中工作?

[英]How do I make my JavaScript work within repeater?

I have an asp.net project using a repeater. 我有一个使用中继器的asp.net项目。

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
        <HeaderTemplate>
            <asp:label ID="lblexp" runat="server" Text="05/11/1981" Visible="false"></asp:label>
        </HeaderTemplate>
        <ItemTemplate>                
            <div id="myNest" class="grid">
            <div class="imgholder">
                <img src="<%# Eval("ImageAlt1")%>" />
            </div>        
            <strong><%# Eval("ItemName")%> <bdo style="color:green;">$<%# Eval("ItemPrice")%></bdo></strong>
            <p class="test"><%# Eval("ItemDescription")%></p>
            <div style="color:red; font-size:small;"><asp:Label ID="lblExp" Text='<%# Bind("CreateDate") %>' runat="server" /></div>
            <div class="meta"><%# Eval("Email")%></div>
            </div>
        </ItemTemplate>
        <FooterTemplate>
            <asp:label ID="lblexp" runat="server" Text="05/11/1981" Visible="false"></asp:label>
        </FooterTemplate>

and some javascript to shorten the ItemDescription 和一些JavaScript来缩短ItemDescription

<script type="text/javascript">
    var after = 5;
    var html = $(".grid p.test").html();
    html = html.substring(0, after) + "<span> ...</span>";
    $(".grid p.test").html(html);
</script>

For whatever reason the javascript cannot see the items within the repeater. 无论出于何种原因,javascript都无法看到转发器中的项目。 How can I fix this? 我怎样才能解决这个问题?

The error is Uncaught TypeError: Cannot read property substring of undefined 错误是未捕获的TypeError:无法读取未定义的属性子字符串

Two possible issues here: 这里有两个可能的问题:

  1. JS runs before relevant markup is rendered. JS在呈现相关标记之前运行。 To fix that, just wrap your js in some kind of document load handler: 要解决此问题,只需将您的js包装在某种文档加载处理程序中:

     $(function() { var after = 5; var html = $(".grid p.test").html(); html = html.substring(0, after) + "<span> ...</span>"; $(".grid p.test").html(html); }); 
  2. html() call gives you inner html of the first item in the list of selector matches. html()调用为您提供选择器匹配列表中第一项的内部html。 You might what to do this for all found items, in which case put your html manipulation in each() . 您可能要对所有找到的项目执行此操作,在这种情况下,请将html操作放入each() And consider text instead of html by the way, it looks more relevant to your case. 顺便说一下,考虑text而不是html ,它看起来与您的案例更相关。

I suspect your script is running before the DOM is loaded. 我怀疑您的脚本在加载DOM之前正在运行。 Either move that script tag to just above your </body> tag or wrap your script like this: 将该脚本标记移到</body>标记的上方,或者像这样包装您的脚本:

<script type="text/javascript">
  $(function () { // equivalent to $(document).on('ready', function...
    var after = 5;
    var html = $(".grid p.test").html();
    html = html.substring(0, after) + "<span> ...</span>";
    $(".grid p.test").html(html);
  });
</script>

The function passed in will only run after the DOM is ready to be parsed. 传入的函数仅在准备好解析DOM之后才能运行。 Meaning your logic won't run until the whole page is ready. 这意味着直到整个页面准备就绪,您的逻辑才会运行。

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

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