Php 将类添加到标题属性上有文本的每个元素

Php 将类添加到标题属性上有文本的每个元素,php,jquery,Php,Jquery,我正在动态生成标题属性的内容: <div class="point_data"> <a href="#" title="<?php the_sub_field('points_description'); ?>"></a> 如何将class.point工具提示添加到.points\u数据div中标题属性上有文本(换句话说,标题属性不是空的)的每个锚定链接?使用以下方法: $('.point_data a[title]').addClass('po

我正在动态生成标题属性的内容:

<div class="point_data">
<a href="#" title="<?php the_sub_field('points_description'); ?>"></a>

如何将class.point工具提示添加到.points\u数据div中标题属性上有文本(换句话说,标题属性不是空的)的每个锚定链接?

使用以下方法:

$('.point_data a[title]').addClass('point-tooltip');
如果标题可以为空,请使用以下选项:

$('.point_data a[title][title!=""]').addClass('point-tooltip');
:

描述:选择不具有指定属性或具有指定属性但不具有特定值的元素

使用以下命令:

$('.point_data a[title]').addClass('point-tooltip');
如果标题可以为空,请使用以下选项:

$('.point_data a[title][title!=""]').addClass('point-tooltip');
:

描述:选择不具有指定属性或具有指定属性但不具有特定值的元素

试试这个

 $(".point_data").find("a[title]").each(function(){
    var titleval = $(this).attr("title").trim();
    if(titleval.length != 0)
    {
        $(this).attr("class","point-tooltip");
    }
});
请参阅此JSFIDLE链接: 试试这个

 $(".point_data").find("a[title]").each(function(){
    var titleval = $(this).attr("title").trim();
    if(titleval.length != 0)
    {
        $(this).attr("class","point-tooltip");
    }
});
请参阅此JSFIDLE链接:

如果您想在PHP中执行此操作:

<div class="point_data">
<?php 
$title = the_sub_field('points_description');
if(empty($title)) { ?>
  <a href="#" >BLAA</a>
<?php }
else { ?>
  <a href="#" class="point-tooltip" title="<?php echo $title ?>">BLAA</a>
<?php } ?>

如果要在PHP中执行此操作:

<div class="point_data">
<?php 
$title = the_sub_field('points_description');
if(empty($title)) { ?>
  <a href="#" >BLAA</a>
<?php }
else { ?>
  <a href="#" class="point-tooltip" title="<?php echo $title ?>">BLAA</a>
<?php } ?>

这会将类添加到A而不是div。如何将类添加到.points\u数据中的每个锚链接。point工具提示您是对的,我读错了。但是应该删除[title],因为OP states title属性,换句话说,它不是空的?。这将向其中添加类。这将向A添加类,而不是向div添加类。指向.points\u数据内每个锚链接的工具提示你是对的,我读错了。但是应该删除[title],因为OP states title属性不是空的?。这会将类添加到其中,但不能解决将类添加到锚的解决方案。@ErikPhilips。添加了该位。如果标题可以为空,则使用[title!=]正确,这是否意味着如果标题不是空的???@ErikPhilips。我的意思是,如果title属性存在一个选项,那么它就没有像或这样的值。现在清楚了吗?这并不能解决向锚添加类的问题。@ErikPhilips。添加了该位。如果标题可以为空,则使用[title!=]正确,这是否意味着如果标题不是空的???@ErikPhilips。我的意思是,如果title属性存在一个选项,那么它就没有像或这样的值。现在清楚了吗?您应该使用addClass而不是attr,只需一个简单的选择器即可。您应该使用addClass而不是attr,只需一个简单的选择器即可。