多个 jquery 选择器

发布于 2024-10-21 11:30:03 字数 999 浏览 1 评论 0原文

感谢 karim79 我能够单击 ImageButton 并将 Jquery 高亮效果应用于不同的 div

$("#btnFavorite").click(function() {
    // selector for element to highlight
    $("#theDiv").effect("highlight", {}, 3000);
});

现在我想将问题扩展如下。

我动态地将 ImageButtons 添加到网页,并且我想在每次 ImageButton 点击时将效果应用到 div 上。

     <asp:ListView ID="ListView1" runat="server">
        <layouttemplate>
            <asp:PlaceHolder id="itemPlaceholder" runat="server" />
        </layouttemplate>
        <ItemTemplate> 
        <asp:ImageButton ID="btnFavorite" runat="server" ImageUrl="~/Images/Favorite.png"/>
        </ItemTemplate>
    </asp:ListView>

这种情况我该怎么办?通过使用列表视图的 ItemDataBound 并添加属性 像

btnFavorite.Attributes.Add("onmouseclick", "doSomething") 或者什么?

我完全迷路了!

Thanks to karim79 i am able to click on the ImageButton and apply the Jquery highlight effect to a different div

$("#btnFavorite").click(function() {
    // selector for element to highlight
    $("#theDiv").effect("highlight", {}, 3000);
});

Now i would like to extend the question as follows.

I add the ImageButtons to the webpage dynamically, and i would like to apply the effect on the div for every ImageButton click.

     <asp:ListView ID="ListView1" runat="server">
        <layouttemplate>
            <asp:PlaceHolder id="itemPlaceholder" runat="server" />
        </layouttemplate>
        <ItemTemplate> 
        <asp:ImageButton ID="btnFavorite" runat="server" ImageUrl="~/Images/Favorite.png"/>
        </ItemTemplate>
    </asp:ListView>

What should i do in that case? By using ItemDataBound of the listview and adding attributes
like

btnFavorite.Attributes.Add("onmouseclick", "doSomething") or what?

I am totally lost!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挖个坑埋了你 2024-10-28 11:30:03

您可以尝试像这样的更通用的选择器...

$("input[type='image']").click(function(){
    $(".. related div selector ..").effect("highlight", {}, 3000);
});

我不知道 divinput 之间的关系,所以我无法假设选择器是什么,但是如果您发布关系或解释它,我们可以帮助您编写更准确的选择器。

值得注意的是,如果您在 DOM 加载后将 ImageButton 动态添加到页面而不是在服务器端,那么您可能需要使用 live 用于附加事件的方法...

$("input[type='image']").live('click', function(){
    $(".. related div selector ..").effect("highlight", {}, 3000);
});

You could try a more generic selector like this...

$("input[type='image']").click(function(){
    $(".. related div selector ..").effect("highlight", {}, 3000);
});

I don't know the relationship between the div and the input so I can't assume what the selector is but if you post the relationship or explain it we can help you write a more accurate selector.

It should be worth noting that if you are dynamically added the ImageButtons to the page after the DOM has loaded and not on the server side then you would likely want to use the live method for attaching events...

$("input[type='image']").live('click', function(){
    $(".. related div selector ..").effect("highlight", {}, 3000);
});
木格 2024-10-28 11:30:03

只需将名为 class 的属性添加到您想要具有此效果制作器功能的 ImageButtons 中,并为其指定一个名称,例如 effectMaker。然后调用相同的函数,但将选择器更改为

$(".effectMaker").click(function() {
    // selector for element to highlight
    $("#theDiv").effect("highlight", {}, 3000);
});

使用 . 选择器后跟 stringValue,您可以引用具有以该 stringValue< 值表示的类的每个元素/em>。

Simply add an attribute named class to the ImageButtons you want to have this effect maker function, and value it with a name, for example effectMaker. Then call the same function, but change the selector to

$(".effectMaker").click(function() {
    // selector for element to highlight
    $("#theDiv").effect("highlight", {}, 3000);
});

With the . selector followed by a stringValue, you reference every element that has a class valued with that stringValue.

清风挽心 2024-10-28 11:30:03

有人说委托功能比实时功能更好:

http://test.kingdesk.com/jquery /bind_live_delegate.php

Some say the delegate function is better than the live function:

http://test.kingdesk.com/jquery/bind_live_delegate.php

往事风中埋 2024-10-28 11:30:03

我不知道这是否适合您,但我所做的是:

$("#<%= ListView1.ClientID %>").find(".ImageButtonTarget").click(function() {
   ..
});

我为我的按钮控件提供一个唯一的类来查找它,并批量解析整个列表视图。我不确定是否有一个或多个 div,但如果 div 位于 listview 行中,您还可以使用父项的组合并 find 也从 ListView 行中获取该 div。

这也将范围限制在 ListVIew 内;选择器不必解析整个页面来查找图像按钮,只需在列表中即可。

HTH。

I don't know if this will work for you, but what I've done is this:

$("#<%= ListView1.ClientID %>").find(".ImageButtonTarget").click(function() {
   ..
});

I give my button control a unique class to find it with, and parse the entire list view in bulk. I'm not sure if there is one or many divs, but if the div is in the listview row, you can also use a combination of parents and find to grab that from the ListView row too.

This also keeps the scoping to within the ListVIew; the selector doesn't have to parse the entire page to find the image buttons, only within the list.

HTH.

他是夢罘是命 2024-10-28 11:30:03

这会将effect()应用于“theDiv”容器中“image”类型的每个输入:

$('#theDiv').find('input[type="image"]').effect('highlight', {}, 3000);

或者,这会将其应用于每个输入类型图像点击上的“theDiv”:

$('input[type="image"]').click(function() {
  $('#theDiv').effect('highlight', {}, 3000);
});

不确定您要执行哪一个。

This will apply effect() on every input of type 'image' within the 'theDiv' container:

$('#theDiv').find('input[type="image"]').effect('highlight', {}, 3000);

Or, this will apply it to 'theDiv' on every input type image click:

$('input[type="image"]').click(function() {
  $('#theDiv').effect('highlight', {}, 3000);
});

Not sure which one you're trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文