【问题标题】:Can jquery traverse the entire DOM and find the word Update in a hrefjquery能否遍历整个DOM并在href中找到Update这个词
【发布时间】:2016-01-20 22:46:24
【问题描述】:

似乎人们在板上单击 Enter 键,虽然我可以禁用,但我有业务分析师希望我让 Enter 键“单击”更新链接

由于这是 asp.net gridview,我没有太多的控制权,而且有很多行的 __doPostBack 数据不同

如何检测“更新”一词(它是“编辑”以及许多其他具有该编辑链接的行)

<a href="javascript:__doPostBack(&#39;GridView1$_ctl2$_ctl0&#39;,&#39;&#39;)">Update</a>

更新:

我想在表中的 href 中查找关键字。

所以“更新”是在href中的关键字,在

<table>
   <tr>
      <td>
          <a href..>Update</a>
      </td>
   </tr>
</table>

更新 2:

这是一个小提琴 http://jsfiddle.net/bthorn/vzjLzfck/

我相信如果有更新,这确实会查找

 var y = $('a').filter(function(index) { return $(this).text() === "Update"; });

 I changed Update to "Updated" and length went from 1 to 0 .... 

那么,如果这有效,我想继续执行该链接或执行其他操作,就像有人点击了那个 href 链接

最终工作代码

$(document).keypress(function (e) {
        if (e.which == 13) {

            $("a").each(function () {

                $("a").filter(function () {
                    var s = $(this).text() === "Update";

                    if (s == true) {
                        //console.log('yes in');
                        this.click();
                    }

                });


                return false;
            });

            return false;
        }
    });

【问题讨论】:

  • 我没有使用 GridViews 的经验,但如果你想获取所有包含文本 Update 的 'a' 标签,你可以使用 $('a:contains("Update")') 或精确的匹配 $('a').filter(function(index) { return $(this).text() === "Update"; }).
  • 所以“更新”在 href 属性中?
  • 如果我误解了您的问题,请告诉我,但如果“更新”将出现在 href 属性中,您可以将过滤器更改为:$('a').filter(function(index) { return $(this).attr('href').indexOf("Update") > -1 })
  • 是的,我认为您的第二个“完全匹配”可能会起作用我将再次编辑我的问题以粘贴一个 jsfiddle 以及提前提出问题,然后请计划添加一个答案那么我最终可以给你积分等......
  • console.log 我可以看到 href:"javascript:__doPostBack('GridView1$_ctl2$_ctl0','')"

标签: javascript jquery asp.net


【解决方案1】:

如果您想定位任何包含“更新”一词的href,您可以在href 上使用jQuery 的filter 函数

$("a").filter(function(){
    return $(this).attr('href').indexOf("Update");
});

如果您希望文本包含“更新”,您可以使用:

 $("a").filter(function(){
    return $(this).text() === "Update";
 });

【讨论】:

  • 如何检测到这些?它如何执行href?
  • 当 Enter 被击中时,您是否能够确定哪个 GridView 行具有焦点?因为如果您知道该行,您可以定位链接并单击它 $(row).find('a').filter(return $(this).text() === "Update";).click() ;
  • 我不知道哪一行,但似乎 href: "javascript:__doPostBack('GridView1$_ctl2$_ctl0','')" 是单击时在 jsfiddle 控制台窗口上显示的内容在提交按钮上,它本质上显示您的 var y = $('a').filter(function(index) { return $(this).text() === "Update"; });
  • 只有一行会有Update,怎么调用.click()呢?
【解决方案2】:
$("body").on("keypress", function(e) {
    if(e.which != 13) {
       return;
    };
    $("a").each(function() {
        var link = $(this);
        if(link.text() == "Update") {
            console.log("woot");
            window.open(link.attr("href"));
        };
    });
});

【讨论】:

  • 我认为它不知道 this.text,即使代码看起来合乎逻辑
  • 我看到小提琴在工作,但是 Callout 是我的代码在执行 console.log(this); 时发生的事情;跨度>
  • 你可以尝试使用if($(this).text() == "Update")if($(this).html() == "Update") 代替
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2014-06-30
  • 1970-01-01
  • 2010-10-13
  • 1970-01-01
  • 2013-06-14
相关资源
最近更新 更多