想要在<li>元素中附加工具提示内容

时间:2016-05-09 12:42:07

标签: jquery

<div>
  <ul>
    <li>This is first point</li>
    <li>This is second point <a href="http://" title="This is more tooltip">More</a></li>
  </ul>
</div>

我想使用jquery在li元素中附加锚标签标题文本,这样我就可以得到没有超链接的纯文本。

例如,处理后li元素将为

<li>This is second point This is more tooltip</li>

3 个答案:

答案 0 :(得分:0)

这可以满足您的需求。如果向元素添加ID,则调整为更简单。

$fileName = 'etykieta_' . $package_no . '.' . $xml->label->format;
file_put_contents($fileName, base64_decode($xml->label->base64));

JSFiddle

答案 1 :(得分:0)

尝试使用标题:

<li id="yourElementId">example</li>

使用Javascript:

document.getElementById('yourElementId').title = 'your new title';

答案 2 :(得分:0)

我就是这样做的(代码中的注释来解释发生了什么):

$('li').each(function() {  // loop through each li - you can change this selector to match your needs
  var li = $(this),
      anchors = li.find('a'); // in case there are multiple anchors?
  
  anchors.each(function() {
    var anchor = $(this),
        title = anchor.attr('title');
    
    if (typeof title !== typeof undefined && title !== false) { // do this if so only if the title attribute exists do you remove the anchor
      anchor.after(title);  // place the title after the link so it keeps it's place in the li (in case there is text after the link).  If you just want the text at the end of the li use li.append(title);
      anchor.detach();   // remove the anchor - move this to after the if statement if you want to remove the anchor and it's text regardless of whether or not it has a title.  If you want to keep the text in the anchor, just remove this line altogether
    }
  });
  
  // li.text(li.text()); // remove all other html within li (optional)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<ul>
    <li>This is first point</li>
    <li><strong>This is second point</strong> <a href="http://" title="This is more tooltip">More</a></li>
</ul>
</div>