Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何确定TD是否只有文本,下一个TD是否包含以特定ID开头的控件_Jquery - Fatal编程技术网

Jquery 如何确定TD是否只有文本,下一个TD是否包含以特定ID开头的控件

Jquery 如何确定TD是否只有文本,下一个TD是否包含以特定ID开头的控件,jquery,Jquery,我不是JQuery专家,需要一些帮助 我有一个表单,我试图确定一个TD单元格是否只有文本(文字控件),下一个TD是否有一个包含以“UI”开头的id的控件。这应该在用户将鼠标悬停在文本上时发生 例如: <table> <tr> <td><asp:Literal ID="LSomeID" runat="server"></asp:Literal></td> <td><as

我不是JQuery专家,需要一些帮助

我有一个表单,我试图确定一个
TD
单元格是否只有文本(文字控件),下一个
TD
是否有一个包含以“UI”开头的
id
的控件。这应该在用户将鼠标悬停在文本上时发生

例如:

<table>
    <tr>
        <td><asp:Literal ID="LSomeID" runat="server"></asp:Literal></td>
        <td><asp:DropDownList ID="UISomeID" runat="server"></asp:DropDownList></td>
    </tr>
</table>

我试图在用户悬停文字时显示工具提示。当他们确实需要查看next
TD
并获取以“UI”开头的id时,然后执行数据库查找

非常感谢您的帮助。

工具提示:

<div style="position: absolute; border: 1px solid black; display: none" id="tooltip">
  Tooltip
</div>

代码:-已更新

var$tds=$('tbl td'),
模式='3';//我们将搜索的模式。
$tds.每个(函数(){
var$th=$(此),
$children=$th.children(),
$content=$th.text();
//如果内容存在且为文本
//检查该模式是否存在
如果($content&&$children.length==0){
如果($content.indexOf(pattern)!=-1){
var$next=$th.next(),
$thisID,$nextID;
//向当前和下一个TD添加类
$th.addClass('contains');
$next.addClass('next');
//获取当前和下一个TD的ID
//如果当前TD是其TR中的最后一个TD
//然后返回“no next”。
$thisID=$th.attr('id');
$nextID=$next.attr('id')?$next.attr('id'):'No next';
log('ThisID:'+$ThisID);
log('NextID:'+$NextID);
}	
console.log('-----------------------');
}
});
表#tbl td{边框:1px纯黑色;}
.包含{背景色:橙色;轮廓:2px海军纯色;}
.next{背景色:绿色;颜色:白色;}

项目1.1
项目1.2
项目1.3
项目1.4
项目2.1
项目2.2
项目2.3
项目2.4
项目3.2
项目3.4
项目3.5


请注意,未选择第三个tr中的第一个td,因为它是空的

还请注意,td4 tr3中的项目3.5未被选中,因为它不包含
纯文本它有一个p标签,包装文本

我会这样做,希望它能增加一些价值

  $('table tr').hover(function(){

      var literal  = $(this).find('td:first');

      // check if literal has text and id start with UI of next td to litral is not undefiend(means it exist), also check if first td just contains string not sub elements.

    if(literal.children().length < 0 && literal.text().length > 0 && literal.next(td[id^="UI") != undefiend){

    //make ajax call or db look up

   }
$('table tr')。悬停(函数(){
var literal=$(this.find('td:first');
//检查literal是否有text和id,litral的下一个td的UI是否未定义(意味着它存在),还检查第一个td是否只包含字符串而不是子元素。
if(literal.children().length<0&&literal.text().length>0&&literal.next(td[id^=“UI”)!=Undefined){
//进行ajax调用或db查找
}

只需要一些条件逻辑

$('td').hover(function() {
    var $cell = $(this),
        $next = $cell.next(),
        $prev = $cell.prev();
    // if no tags as children it is text only
    if (!$cell.children().length) {
        // see if neighbors have wanted ID
        if ($next.find('[id^="UI"]').length || $prev.find('[id^="UI"]').length) {
            //show tooltip
        }
    }

}, function() {
    //hide tooltip
});
或者相反,如果您需要在这些单元格上使用插件,则可以使用
filter()

或者,这里有一种使用已知ID的方法:

$('td:has(select[id^="UI"])').each(function(){
   var $cell= $(this), $prev = $cell.prev(), $next=$cell.next();
   if(!$prev.children().length){
        $prev.mytoolTip()
   }
   if(!$next.children().length){
        $next.mytoolTip()
   }       
});

谢谢。不,如果td的鼠标悬停处只有文本,它应该只返回下一个td的id。如果我正确理解了您的问题,请将
literal
转换为span。您可以使用
$(“表tr span”)找到这个
当我查看源代码时,它会显示一些文本。我如何确保没有其他元素或控件,只有文本?感谢大家的帮助。我正在编写一组有用的代码。完成后,我将发布最终结果。再次感谢大家这看起来是一个很好的解决方案,但我的答案是100ms和不能全部检查并添加该类来执行td。有没有办法确定它是一个文本控件并动态添加该类,还是td只包含文本而不包含其他内容。再次感谢谢谢。如果我可以替换此if($content.match('3'),这可能会起作用要确定单元格是否只包含文本,我不知道每个文本将包含什么。让我做一个工作示例来检查内容是否为文本only@ches185现在它正在工作,检查td是否为空,以及它是否只有文本谢谢。这个文本()如果td中存在其他元素和控件,则删除它们?更新的代码使用literal.children()检查第一个td是否只包含字符串而不是子元素。length<0不清楚
next
的含义,因此我从两个方向查看。在DOM术语中
next
始终是以下元素
$('td').filter(function() {
    var $cell = $(this),
        $next = $cell.next(),
        $prev = $cell.prev(),
        hasChildren = $cell.children().length;

        return !hasChildren && ($next.find('[id^="UI"]').length || $prev.find('[id^="UI"]').length);


}).mytoolTipPlugin();
$('td:has(select[id^="UI"])').each(function(){
   var $cell= $(this), $prev = $cell.prev(), $next=$cell.next();
   if(!$prev.children().length){
        $prev.mytoolTip()
   }
   if(!$next.children().length){
        $next.mytoolTip()
   }       
});