Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript InnerHTML+;find()不工作_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript InnerHTML+;find()不工作

Javascript InnerHTML+;find()不工作,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我一直在搜索和尝试不同的代码,但我没有找到解决方案 我正在尝试创建的脚本执行以下操作: 当用户使用class=“Box”在div内单击时。程序将在变量中保存框的内容 var boxContent = $(this).parents('.Box')[0].innerHTML; 因此,变量boxContentnow的值(根据我的html输出将是)返回用户单击的孔div: <div class="Box"> <div class="URL_ID">

我一直在搜索和尝试不同的代码,但我没有找到解决方案

我正在尝试创建的脚本执行以下操作:

  • 当用户使用class=“Box”在div内单击时。程序将在变量中保存框的内容

    var boxContent = $(this).parents('.Box')[0].innerHTML;
    
  • 因此,变量boxContentnow的值(根据我的html输出将是)返回用户单击的孔div:

        <div class="Box">
           <div class="URL_ID">
               <span>http://test.com</span>
               <span id="ID">3232434</span>
           </div>
           <div class="info">
                <a href="#"></a>
           </div>
           <div class="secondLink">
                <a href="#"></a>
           </div>
        </div>
    
    我能做什么?? 提前感谢。

    您的声明:

    因此,变量boxContent now的值(根据我的html输出将是)返回用户单击的孔div:

        <div class="Box">
           <div class="URL_ID">
               <span>http://test.com</span>
               <span id="ID">3232434</span>
           </div>
           <div class="info">
                <a href="#"></a>
           </div>
           <div class="secondLink">
                <a href="#"></a>
           </div>
        </div>
    

    事实并非如此
    innerHTML
    返回元素内的标记html字符串。要使用Jquery的
    .find()
    方法,必须实际选择Jquery对象:

    var url = $(this).parents('.Box').find('.BoxSongUrl');
    
    var url = $(boxContent).find('.BoxSongUrl');
    

    您可以使用
    clone
    在运行查询时保留第一个“.Box”元素的副本

    var boxContent = $(this).parents('.Box').first().clone();
    
    然后,您可以在
    boxContent

    var url = boxContent.find('.BoxSongUrl');
    
    编辑:

    如果您不想克隆整个jQuery对象,您可以保存html并在调用find之前重新解析它

    var boxContent = $(this).parents('.Box').first().html();
    
    然后,将HTML重新解析为jQuery对象:

    var url = $(this).parents('.Box').find('.BoxSongUrl');
    
    var url = $(boxContent).find('.BoxSongUrl');
    

    html
    innerHTML
    中没有
    BoxSongUrl
    返回没有
    find
    方法的字符串!与问题无关,但对于
    innerHTML
    使用jQuery
    .html()
    方法。e、 g.
    $(this).parents('.Box').html()
    。您只需要对outerHTML使用DOM元素。如果您只需要jQuery返回的第一个元素,可以使用
    .first()
    而不是
    [0]
    。不知道这是否真的重要。他为什么会那样做???创建div的完整副本,只是为了执行搜索?@LcSalazar别问我,问题是OP希望保存内容,稍后再搜索。大概是因为内容在未来代码执行时已经更改了。这就是你要做的。-1很明显,OP知道如何在一行中完成这一切。你似乎错过了一个问题:“现在我想要的是稍后处理的div…但不起作用”。对不起@PrestonS,但你错过了重点!您不必克隆div(如您所建议的),也不必存储其html字符串标记(如OP所尝试的)。Jquery对象是对实际DOM元素的引用,可以对其进行导航、操作和搜索。。。在我的回答中,存储
    find
    结果就足够了,因为您可以稍后随时调用父
    .Box
    !如果dom元素在被访问时发生了变化,该怎么办?