如何通过jQuery隐藏两个特定id之间的内容?

时间:2015-11-18 11:02:47

标签: javascript jquery html

我必须隐藏具有不同ID的两个特定div之间的所有内容。

例如:

<div id="a1">Hi</div>
<div>Some Random Text</div>
<div id="a2">Hello</div>

与上例相似,我想隐藏id a1和a2之间的div。

7 个答案:

答案 0 :(得分:3)

您可以使用nextUntil()实现此目的。在一行中:

$("#a1").nextUntil("#a2").hide();

示例:

$(document).ready(function() {
  $("#hide").click(function() {
    var hideElement = $("#a1").nextUntil("#a2");
    hideElement.hide();
  });
});
<div id="a1">Hi</div>
<div>Some Random Text</div>
<div>Some Random Text</div>
<div id="a2">Hello</div>
<button id="hide">hide</button>

jsFiddle

请注意,nextUntil()不包含杂散文本节点,请参阅this question以获取解决方案。

答案 1 :(得分:2)

nextAlleach一起使用。

&#13;
&#13;
$('#a1').hide().nextAll().each(function() {
  $(this).hide();

  if ($(this).attr('id') === 'a2') {
    return false;
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>HELLO</div>
<div id="a1">Hi</div>
<div>Some Random Text</div>
<div id="a2">Hello</div>
<div>BYE</div>
&#13;
&#13;
&#13;

答案 2 :(得分:2)

现在你可以在// Dropzone class: var myDropzone = new Dropzone("div#myId", { url: "/file/post"}); // jQuery $("div#myId").dropzone({ url: "/file/post" });

中隐藏它

&#13;
&#13;
css
&#13;
    #a1, #a1 ~ #a2{display:none;}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

如果cotrols都是兄弟姐妹,您可以使用.nextUntil().prevUntil() jQuery选择器。有关详细信息,请参阅thisthis 但是,如果控件不共享同一个父级,则没有简单的选择器。您必须创建一种方法来查找控件的父级,并且可以找到他们的父母等。

答案 4 :(得分:0)

使用简单的jquery,

$("#a1+div").hide(); 

或使用css

#a1+div{
display: none;
}

如果你想删除这两个div之间的所有内容,试试这个,

$("#a1").nextAll().each(function(){
    if($(this).attr("id") != "a2"){
        $(this).hide();
    }
    else{
        //element found with id 'a2' so exit;
        return false;
    }
});

答案 5 :(得分:0)

如果你只使用css隐藏它会更好。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace EncryptXmlTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            RijndaelManaged key = null;

            try
            {
                // Create a new Rijndael key.
                key = new RijndaelManaged();
                // Load an XML document.
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.PreserveWhitespace = true;
                xmlDoc.Load("test.xml");

                // Encrypt the "creditcard" element.
                //Encrypt(xmlDoc, "creditcard", key);
                EncryptMultiple(xmlDoc, "creditcard", key);

                Console.WriteLine("The element was encrypted");

                Console.WriteLine(xmlDoc.InnerXml);

                //Decrypt(xmlDoc, key);
                DecryptMultiple(xmlDoc, key);

                Console.WriteLine("The element was decrypted");

                Console.WriteLine(xmlDoc.InnerXml);
                Console.ReadKey();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                // Clear the key.
                if (key != null)
                {
                    key.Clear();
                }
            }
        }

        public static void Encrypt(XmlDocument Doc, string ElementName, SymmetricAlgorithm Key)
        {
            // Check the arguments.  
            if (Doc == null)
                throw new ArgumentNullException("Doc");
            if (ElementName == null)
                throw new ArgumentNullException("ElementToEncrypt");
            if (Key == null)
                throw new ArgumentNullException("Alg");

            ////////////////////////////////////////////////
            // Find the specified element in the XmlDocument
            // object and create a new XmlElemnt object.
            ////////////////////////////////////////////////
            XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementName)[0] as XmlElement;
            XmlNodeList elementsToEncrypt = Doc.GetElementsByTagName(ElementName);
            // Throw an XmlException if the element was not found.
            if (elementsToEncrypt == null)
            {
                throw new XmlException("The specified elements were not found");

            }

            //////////////////////////////////////////////////
            // Create a new instance of the EncryptedXml class 
            // and use it to encrypt the XmlElement with the 
            // symmetric key.
            //////////////////////////////////////////////////

            EncryptedXml eXml = new EncryptedXml();

            byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, Key, false);

            ////////////////////////////////////////////////
            // Construct an EncryptedData object and populate
            // it with the desired encryption information.
            ////////////////////////////////////////////////

            EncryptedData edElement = new EncryptedData();
            edElement.Type = EncryptedXml.XmlEncElementUrl;

            // Create an EncryptionMethod element so that the 
            // receiver knows which algorithm to use for decryption.
            // Determine what kind of algorithm is being used and
            // supply the appropriate URL to the EncryptionMethod element.

            string encryptionMethod = null;

            if (Key is TripleDES)
            {
                encryptionMethod = EncryptedXml.XmlEncTripleDESUrl;
            }
            else if (Key is DES)
            {
                encryptionMethod = EncryptedXml.XmlEncDESUrl;
            }
            if (Key is Rijndael)
            {
                switch (Key.KeySize)
                {
                    case 128:
                        encryptionMethod = EncryptedXml.XmlEncAES128Url;
                        break;
                    case 192:
                        encryptionMethod = EncryptedXml.XmlEncAES192Url;
                        break;
                    case 256:
                        encryptionMethod = EncryptedXml.XmlEncAES256Url;
                        break;
                }
            }
            else
            {
                // Throw an exception if the transform is not in the previous categories
                throw new CryptographicException("The specified algorithm is not supported for XML Encryption.");
            }

            edElement.EncryptionMethod = new EncryptionMethod(encryptionMethod);

            // Add the encrypted element data to the 
            // EncryptedData object.
            edElement.CipherData.CipherValue = encryptedElement;

            ////////////////////////////////////////////////////
            // Replace the element from the original XmlDocument
            // object with the EncryptedData element.
            ////////////////////////////////////////////////////

            EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
        }

        public static void EncryptMultiple(XmlDocument Doc, string ElementName, SymmetricAlgorithm Key)
        {
            // Check the arguments.  
            if (Doc == null)
                throw new ArgumentNullException("Doc");
            if (ElementName == null)
                throw new ArgumentNullException("ElementToEncrypt");
            if (Key == null)
                throw new ArgumentNullException("Alg");

            XmlNodeList elementsToEncrypt = Doc.GetElementsByTagName(ElementName);

            if (elementsToEncrypt == null)
            {
                throw new XmlException("The specified elements were not found");
            }

            //XmlNodeList elementsToEncryptCloned = Doc.GetElementsByTagName(ElementName);
            List<string> ids = new List<string>();

            for (int i = 0; i < elementsToEncrypt.Count; i++)
            {
                string attrVal = elementsToEncrypt[i].Attributes["id"].Value;
                ids.Add(attrVal);
            }

            foreach(string id in ids)
            {
                string query = string.Format("//*[@id='{0}']", id);
                XmlElement elementFetched = (XmlElement)Doc.SelectSingleNode(query);

                if (elementFetched == null)
                    continue;

                EncryptedXml eXml = new EncryptedXml();
                byte[] encryptedElement = eXml.EncryptData(elementFetched, Key, false);

                EncryptedData edElement = new EncryptedData();
                edElement.Type = EncryptedXml.XmlEncElementUrl;
                edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
                edElement.CipherData.CipherValue = encryptedElement;

                EncryptedXml.ReplaceElement(elementFetched, edElement, false);
            }

        }

        public static void Decrypt(XmlDocument Doc, SymmetricAlgorithm Alg)
        {
            // Check the arguments.  
            if (Doc == null)
                throw new ArgumentNullException("Doc");
            if (Alg == null)
                throw new ArgumentNullException("Alg");

            // Find the EncryptedData element in the XmlDocument.
            XmlElement encryptedElement = Doc.GetElementsByTagName("EncryptedData")[0] as XmlElement;

            // If the EncryptedData element was not found, throw an exception.
            if (encryptedElement == null)
            {
                throw new XmlException("The EncryptedData element was not found.");
            }

            // Create an EncryptedData object and populate it.
            EncryptedData edElement = new EncryptedData();
            edElement.LoadXml(encryptedElement);

            // Create a new EncryptedXml object.
            EncryptedXml exml = new EncryptedXml();


            // Decrypt the element using the symmetric key.
            byte[] rgbOutput = exml.DecryptData(edElement, Alg);

            // Replace the encryptedData element with the plaintext XML element.
            exml.ReplaceData(encryptedElement, rgbOutput);

        }

        public static void DecryptMultiple(XmlDocument Doc, SymmetricAlgorithm Alg)
        {
            // Check the arguments.  
            if (Doc == null)
                throw new ArgumentNullException("Doc");
            if (Alg == null)
                throw new ArgumentNullException("Alg");

            XmlNodeList encryptedElements = Doc.GetElementsByTagName("EncryptedData");

            if (encryptedElements == null)
            {
                throw new XmlException("The EncryptedData elements were not found.");
            }

            List<XmlNode> nodes = new List<XmlNode>();

            foreach (XmlNode n in encryptedElements)
            {
                if (n == null)
                    continue;

                nodes.Add(n.Clone());
            }

            foreach (XmlElement ele in nodes)
            {
                XmlElement elementFetched = encryptedElements[0] as XmlElement;

                EncryptedData edElement = new EncryptedData();
                edElement.LoadXml(ele);

                EncryptedXml exml = new EncryptedXml();

                byte[] rgbOutput = exml.DecryptData(edElement, Alg);

                exml.ReplaceData(elementFetched, rgbOutput);
            }
        }
    }
}
#a1,#a2 {
display:none;
}

答案 6 :(得分:-1)

IT只使用元素id和hide()函数

&#13;
&#13;
$(document).ready(function(){
  
$("#a1,#a2").hide();

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="a1">Hi</div>
<div>Some Random Text</div>
<div id="a2">Hello</div>
&#13;
&#13;
&#13;