简体   繁体   English

在元素ID下的jQuery append()?

[英]jQuery append() under an element ID?

I am building a navigation bar that is driven based off of values retrieved from a SharePoint List. 我正在构建一个导航栏,该导航栏基于从SharePoint列表检索的值来驱动。 Right now, I am using an <ul> for my column headers and <li> for my contents. 现在,我在列标题中使用<ul> ,在内容中使用<li> I can get the headers to display correctly and can also get the contents of that column to display correctly. 我可以使标题正确显示,也可以使该列的内容正确显示。 What I'm having troubles with is that the <li> seems be appended to the <ul> which is great, but it's also putting it inside of it. 我遇到的麻烦是<li>似乎被附加到了<ul> ,这很棒,但是也将其放入其中。 My <ul> has a border around it and I want the content to be appended directly under that border but instead, it's putting everything inside of it. 我的<ul>周围有一个边框,我希望将内容直接附加在该边框下,但是,它会将所有内容都放在其中。 在此处输入图片说明

Here is the specific block of code I believe is wrong: 这是我认为是错误的特定代码块:

$('#TableElement').hover(function () {
$('[id^=Header]').hover(function () {


    $("#" + this.id).append("<li>" + this.id + "</li>");
});
});

Here is all of my code: 这是我所有的代码:

    ///////////////////////////////////////////////////////////////////////////////////////////    //////////////////////////////////////////
////////////////////////////////////////EVERYTHING BELOW THIS LINE IS GOOD TO    GO/////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////    //////////////////////////////////////////
//Print Headers to Screen. This will drive the core functionalty of the navpart
var siteUrl = '/sites/dev';
ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
theCounter = 0;
var Headers = new Array();
var getCurrentElementId = null;
function retrieveListItems() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('myList');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),  Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    theCounter += 1;
    Headers[theCounter - 1] = oListItem.get_item('Title');
}
var HeaderDisplay = _.uniq(Headers);
for (var i = 0; i <= HeaderDisplay.length - 1; i++) {
    $('#TableElement').append("<th id=Header" + i + ">" + HeaderDisplay[i] + "::::::" +   "</th>");
}
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

///////////////////////////////////////////////////////////////////////////////////////////    //////////////////////////////////////////
////////////////////////////////////////EVERYTHING ABOVE THIS LINE IS GOOD TO    GO/////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////    //////////////////////////////////////////


// You got the headers to print as expected. Right now you need to figure out how to get the current ID
// that the mouse is over. Try looking at another project you did where the mouse goes into the table header
// and the outline expands.


$('#TableElement').hover(function () {
$('[id^=Header]').hover(function () {
  // Come back to this:::::::  var content = $(this).html();


    $("#" + this.id).append("<li>" + this.id + "</li>");
});
});


//This should be the universal onmouseover event that will expose only links
//and values relavent to the selected header.

//$(document).ready(function onPageLoad() {
//    $().SPServices({
//        operation: "GetListItems",
//        async: false,
//        listName: "myList",
//        CAMLQuery: "<Query><Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where></Query>",
//        completefunc: function completeFunction(xData, Status) {
//            $(xData.responseXML).SPFilterNode("z:row").each(function () {
//                var Headers = "<th>" + $(this).attr("ows_Title") + "</th>";
//                $("#TableElement").append(Headers);
//            });
//        }
//    });
//});

HTML Code: HTML代码:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="AnotherMarcPart.VisualWebPart1.VisualWebPart1UserControl" %>
<!DOCTYPE Html />
<html>
<head>
    <link rel="Stylesheet" type="text/css"  href="c:\users\administrator\documents\visual studio 2010\Projects\AnotherMarcPart\AnotherMarcPart\VisualWebPart1\Stylesheet1.css" />
    <title></title>
</head>
<body>
<ul id="TableElement"></ul>
<script type="text/javascript" src="c:\users\administrator\documents\visual studio 2010\Projects\AnotherMarcPart\AnotherMarcPart\VisualWebPart1\jQuery_v1.10.2.js"></script>
<script type="text/javascript" src="c:\users\administrator\documents\visual studio  2010\Projects\AnotherMarcPart\AnotherMarcPart\VisualWebPart1\jquery.SPServices-2013.01.js"></script>
<script type="text/javascript" src="c:\users\administrator\documents\visual studio 2010\Projects\AnotherMarcPart\AnotherMarcPart\VisualWebPart1\Underscore.js 1.5.2.js">    </script>
<script type="text/javascript" src="c:\users\administrator\documents\visual studio 2010\Projects\AnotherMarcPart\AnotherMarcPart\VisualWebPart1\JScript1.js"></script>

</body>
</html>

<li> tags must go inside a <ul> . <li>标记必须放在<ul> If you want your content after the <ul> , then don't put it in an <li> (perhaps put it in a <div> instead) and use jQuery's .after() or .insertAfter() to put it after the <ul> . 如果你想后您的内容<ul>然后不要把它放在一个<li>也许把它放在一个<div>代替),并使用jQuery的.after().insertAfter()把它的后<ul>

Also, this code is likely wrong: 另外,此代码可能是错误的:

$('#TableElement').hover(function () {
$('[id^=Header]').hover(function () {


    $("#" + this.id).append("<li>" + this.id + "</li>");
});
});

You don't want two .hover() handlers inside of one another. 您不希望.hover()两个.hover()处理程序。 You will be installing the second .hover() over and over every time you hover over the first one. 每次将鼠标悬停在第一个.hover()上时,您将.hover()安装。 That will give you lots of duplicate event handlers and the function inside will get executed multiple times. 这将为您提供大量重复的事件处理程序,并且其中的函数将被多次执行。

If .insertAfter() isn't exactly what you want, then show us your HTML and show us exactly where you want the new content inserted. 如果.insertAfter()并非您所需要的,请向我们显示您的HTML,并向我们显示您想要插入新内容的确切位置。 You said you wanted it after the <ul> tag's border so that's what .insertAfter() will do. 您说过要在<ul>标记的边界之后使用它,因此.insertAfter()会这样做。


Based on how you've edited your question, you would append to that other UL like this: 根据您对问题的编辑方式,您可以像这样将其附加到其他UL:

$("#TableElement").append("<li>" + this.id + "</li>");

That will make the <li> be the last <li> inside of the TableElement <ul> which will be inside it's border. 这将使<li>成为TableElement <ul>内部的最后一个<li> <ul> ,它将位于其边界之内。 There is no way to put an <li> inside a <ul> and have the <li> be outside the border around the <ul> . 无法将<li>放在<ul>并使<li><ul>周围的边界之外。 To do that, you would have to create a container object AFTER the <ul> and put the content into that container instead. 为此,您必须在<ul>之后创建一个容器对象,然后将内容放入该容器中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM