简体   繁体   English

jQuery .val()/。text()不适用于特定的html块

[英]jQuery .val()/.text() not working for a specific block of html

I am using HandlebarsJS with Yii and PHP to render a page. 我正在使用带有Yii和PHP的HandlebarsJS渲染页面。 Below is a block of html/handlebars code from the page 下面是页面中的html / handlebars代码块

{{#if embed_link}}
       <p class='f_hidden_p'>
           <a href='{{embed_link}}'>
              {{embed_link}}
            </a>
       </p>
       <div class = 'link-wrapper'>
          <div class = 'link-container'>
              <a class = 'link-anchor-box'>
                  <div class = 'link-pic-wrap'>
                      <div class='playable_wrap'>
                         <div class='play_btn'></div>
                            <div class = 'link-img'></div>
                      </div>
                      <div class = 'link-text-data'>
                          <div class = 'link-text-title'> 
                             <span class = 'link-text-website'></span>
                          </div>
                          <div class = 'link-text-about'></div>
                       </div>
                   </div>
              </a>
            </div>
         </div>
{{/if}}

The above code only runs when embed_link is true. 以上代码仅在embed_link为true时运行。 In my test case it is, and when it runs I need to use jQuery to get the text from "f_hidden_p" . 在我的测试案例中,当它运行时,我需要使用jQuery从“ f_hidden_​​p”获取文本。 So in the js file I have, 所以在js文件中,

$(document).ready(function(){
   console.log($('.f_hidden_p').text());
   console.log($('.f_hidden_p').val());
});

For some reason this returns an empty string or undefined. 由于某种原因,这将返回一个空字符串或未定义。 When I inspect element and check, I can see that the value of {{embed_link}} has a value that is not an empty string. 检查元素并进行检查时,可以看到{{embed_link}}的值不是一个空字符串。 After that I tried, giving the anchor tag an unique id "embed_link" and tried. 之后,我尝试给锚标记赋予唯一的ID“ embed_link”,然后尝试。

$(document).ready(function(){
       console.log($('#embed_link').text());
       console.log($('#embed_link').val());
});

But that didn't work either. 但这也不起作用。 I tried that same syntax with different divs in the HTML that are also within a handlebars block and it worked perfectly. 我在HTML的不同div上尝试了相同的语法,这些语法也在handlebars块内,并且效果很好。 It just doesn't work for this block for some reason. 由于某种原因,它仅对此块无效。 NOTE : .f_hidden_p is hidden -> it's display is 'none' in CSS. 注意:.f_hidden_​​p隐藏->在CSS中显示为“ none”。 But I am quite sure that shouldn't stop .text() or .val() from getting the text. 但是我非常确定,不应阻止.text()或.val()获得文本。

Thanks for the help! 谢谢您的帮助!

I'm adding this for better formatting ... 我正在添加它以获得更好的格式...

Did you try this? 你有尝试过吗?

$(document).ready(function(){
  setTimeout(function() {
   console.log($('.f_hidden_p').text());
   console.log($('.f_hidden_p').val());
  }, 2000);
});

... with no results? ...没有结果?

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

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