简体   繁体   English

需要将链接添加到不带.append()的列表项的文本

[英]Need To Add Link to text of List Item without .append()

console.log(data)
var link
$listSelector = $("#top5CompaniesList") //Your list element
$.each(data.result, function(i, obj) {
    $listSelector.append('<li>');
    link = $('<a href="dashboard.php#companyInfo?'+obj.symbol+'">'+obj.symbol+'</a>').click(function(){
        $('.nav-pills a[href=#companyInfo]').tab('show') ;
        $('body').scrollTop(0);
        $('#companySearchInput').val(obj.symbol);
        $('#companySearchButton').click();
        return false;
    });
    $listSelector.append(link);
    $listSelector.append('<br> Otherdata: '+obj.otherdata+'</li>');         
});

Using the code above I'm trying to add list items to a list that includes a link which I have custom behavior designed for. 使用上面的代码,我试图将列表项添加到包含链接的列表中,该链接具有我设计的自定义行为。 The behavior works, except that there's a line break on the page where each .append() call happens.I'd like the list item to appear as one unbroken line. 这种行为是可行的,除了在每个.append()调用发生的页面上有一个换行符之外,我希望列表项显示为一条连续的行。 I tried using both .wrap() and .wrapInner(), but those seem to replace the entire innerHTML of the list item. 我尝试同时使用.wrap()和.wrapInner(),但是它们似乎替换了列表项的整个innerHTML。 Honestly I know they're probably the solution but I can't seem to wrap my head around the logic. 老实说,我知道这可能是解决方案,但我似乎无法将逻辑包扎起来。 Any help or a push in the right direction is much appreciated. 非常感谢任何帮助或朝正确方向的推动。

The problem is here: 问题在这里:

$listSelector.append('<li>');
// ...
$listSelector.append('<br> Otherdata: '+obj.otherdata+'</li>'); 

First some theory. 首先是一些理论。 You can't add partial HTML to the document. 您不能将部分HTML添加到文档中。 The web page itself doesn't consist of HTML. 网页本身不包含HTML。 It consists of elements, and HTML is only a markup language that represents those elements and tells the browser how to build them. 它由元素组成,HTML只是表示这些元素并告诉浏览器如何构建它们的标记语言。 Appending an open <li> element without closing it doesn't make sense in this perspective: you can't have half of an element in the DOM. 从这个角度看,添加一个打开的<li>元素而不关闭它是没有意义的:DOM中不能有一半的元素。

When you have $listSelector.append('<li>') you're appending an entire <li> element to the document. 当您有$listSelector.append('<li>')$listSelector.append('<li>')整个<li>元素附加到文档中。 It's exactly equivalent to $listSelector.append('<li></li>') . 它完全等同于$listSelector.append('<li></li>') When you later have $listSelector.append('<br> Otherdata: '+obj.otherdata+'</li>') it's exactly equivalent to $listSelector.append('<br> Otherdata: '+obj.otherdata+'<li></li>') – the browser doesn't "remember" that there was an open <li> tag earlier so it "fixes" the broken HTML. 当您以后拥有$listSelector.append('<br> Otherdata: '+obj.otherdata+'</li>')它完全等同于$listSelector.append('<br> Otherdata: '+obj.otherdata+'<li></li>') –浏览器不会“记住”之前有一个打开的<li>标记,因此它“修复”了损坏的HTML。

The extra empty <li> elements are what cause the layout to look broken. 多余的<li>元素是导致布局看起来不完整的原因。

To fix it, first make the <li> element, then add its contents to it, and then append it to the parent: 要修复此问题,请首先创建<li>元素,然后将其内容添加到其中,然后将其附加到父元素中:

link = $('<a href="dashboard.php#companyInfo?'+obj.symbol+'">'+obj.symbol+'</a>').click(function(){
    $('.nav-pills a[href=#companyInfo]').tab('show') ;
    $('body').scrollTop(0);
    $('#companySearchInput').val(obj.symbol);
    $('#companySearchButton').click();
    return false;
});

$('<li>')
    .append(link)
    .append('<br> Otherdata: '+obj.otherdata)
    .appendTo($listSelector);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以交互方式将文本作为列表项附加到HTML文档中的列表而不将列表项存储为javascript变量? - How to append text interactively as a list item to a list in the HTML document without storing the list item as a javascript variable? 添加列表项的URL,并将其添加为按钮的链接 - Add an URL of list item and add it as a link to the button 添加到jquery追加的链接 - add link to jquery append 我的文字没有在Angular js中添加到列表项中? - My text not add in list item in Angular js? 将选定的列表元素添加到textarea中,添加自己的文本 - Append selected list elements into textarea, add own text 单击按钮/文本/链接以对列表项进行排序 <li> 按字母顺序 - Click button/text/link to sort List item <li> Alphabetically 如何将按钮/链接添加到搜索结果列表中的最后一项 - How to add a Button/Link to the last item in a list of search results 如何在vue中的列表中添加一个项目的外部链接? - How to add a external link to a item within a list in vue? 如果没有添加append(“ g”),则无法添加文本,append(“ g”)给出了d3.js的错误 - Can't add text without append(“g”), append(“g”) gives error from d3.js append<li> 如果列表项计数 &lt; 3</li> - append <li> if list item count < 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM