简体   繁体   English

可以追加jquery已经输出的元素吗?

[英]Can you append elements that have already been outputted by jquery?

This is a weird question but can you append/add html to an element that was outputted by jQuery?这是一个奇怪的问题,但是您可以将 html 附加/添加到由 jQuery 输出的元素吗?

So I've been trying to add/append some content, another div element, into a menu.所以我一直在尝试将一些内容(另一个 div 元素)添加/附加到菜单中。 The append works everywhere except for one area (where I actually want to place it) to see if I was doing something wrong (I wasn't).除了一个区域(我实际上想放置它的地方)之外,附加适用于任何地方,以查看我是否做错了什么(我没有做错)。 So after investigating, turns out the area where I want to add it is outputted by another jQuery script.所以经过调查,原来我想添加它的区域是由另一个jQuery脚本输出的。

I can always add the code there but since it's WordPress and it's a core theme file...it will more than likely get overwritten when ever I upgrade the theme.我总是可以在那里添加代码,但因为它是 WordPress 并且它是一个核心主题文件......每当我升级主题时,它很可能会被覆盖。 I also tried to add my script as the very last thing on the footer to give it time.我还尝试将我的脚本添加为页脚的最后一件事,以给它时间。 My guess since it's a dynamic element/menu..the append doesn't work there since it is.我的猜测是因为它是一个动态元素/菜单......因为它是附加在那里不起作用。 I could be wrong.我可能是错的。

I've only tried append and prepend...I know there is other methods to add html/content into an element using jQuery but I wanted to ask the community If its even possible and if I should use something else instead of append?我只尝试过 append 和 prepend ......我知道还有其他方法可以使用 jQuery 将 html/content 添加到元素中,但我想问社区是否可能,我是否应该使用其他东西而不是 append ? Or what would be a better way to add it?或者有什么更好的方法来添加它?

I hope this makes sense.我希望这是有道理的。

The code that I used to append: $( ".av-burger-overlay” ).append( "Test" );我用来附加的代码: $( ".av-burger-overlay" ).append( "Test" );

This code appends except in the menu since the menu is dynamically created when you click on burger menu to open the menu.此代码除了在菜单中附加之外,因为菜单是在您单击汉堡菜单打开菜单时动态创建的。

Little update on this for anyone that stumbles upon this.对于偶然发现此问题的任何人,几乎没有更新。 I was able to append content using Ajax我能够使用 Ajax 附加内容

/**** call second menu****/
            $.ajax(
                {
                    url: "path-to-website/wp-content/themes/idg-child/secondmenu.php", // path to your PHP file
                    dataType:"html",
                    success: function(data)
                {

                    $(data).appendTo(inner_overlay);  //load-into-div is the ID of the DIV where you load the <select>

                } // success
                }); // ajax

this worked out great!效果很好! It's very important to note that in order for any external PHP files to work within wordpress you need to load wordpress like so.需要注意的是,为了让任何外部 PHP 文件在 wordpress 中工作,您需要像这样加载 wordpress,这一点非常重要。

<?php
  define( 'WP_USE_THEMES', false ); // Don't load theme support functionality
  require( '../../../wp-load.php' );
   echo '<div class="secondmenu">';
   echo '<h2 class="secondmenyhead">What We Do</h2>';
   wp_nav_menu( array('menu' => 'Services', 'theme_location'=>'services' ) ); 
   echo '</div>';
  ?>

happy coding :D快乐编码:D

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

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