Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.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 jQuery。单击不被调用_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript jQuery。单击不被调用

Javascript jQuery。单击不被调用,javascript,jquery,ajax,Javascript,Jquery,Ajax,我不熟悉AJAX、jQuery和JavaScript,所以我确信我缺少一些简单的东西。这对我来说是一个很难描述的问题,但我会尽我所能 我有两个html文件,index.html和news.html。我正在使用AJAX更新index.html文件中声明的div(其名称为contentDiv)。我在几个标记上使用class属性将信息传递到jQuery脚本中 当我单击index.html页面上的链接将news.html加载到contentDiv时,它会正确加载。然后单击news.html文件中.pdf

我不熟悉AJAX、jQuery和JavaScript,所以我确信我缺少一些简单的东西。这对我来说是一个很难描述的问题,但我会尽我所能

我有两个html文件,index.html和news.html。我正在使用AJAX更新index.html文件中声明的div(其名称为contentDiv)。我在几个标记上使用class属性将信息传递到jQuery脚本中

当我单击index.html页面上的链接将news.html加载到contentDiv时,它会正确加载。然后单击news.html文件中.pdf文件的链接,什么也没发生。我正在尝试用请求的.pdf替换news.html

我在index.html中有以下内容

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="js/site.js"> </script>
<script src="js/bootstrap.min.js"></script>

<script>

    // Hide the "hideContent" link when the page loads
    $(document).ready(function(){
        $('.hideContent').hide();         // hide the hideContent button

        // AJAX to load pdfs into the contentDiv when the link is clicked
        var text;
        $('.pdf').click(function(){
            text = $(this).attr('text');
            var first = "'<object type=";
            var last = ' width="900" height="450"></object>';
            first = first.concat('"application/pdf" data=');
            first = first.concat(text);
            first = first.concat(last);
            first = first.concat("'");
            var pdfDiv = document.getElementById("contentDiv");
            pdfDiv.innerHTML=first;

            contentDivViewHelper();
            return false;
        })

        var url;
        $('.html').click(function(){
            url = $(this).attr('text');

            processContentDivRequest(url);
            return false;
        })
});//end of document.ready
        </script>

//加载页面时隐藏“hideContent”链接
$(文档).ready(函数(){
$('.hideContent').hide();//隐藏hideContent按钮
//AJAX在单击链接时将PDF加载到contentDiv中
var文本;
$('.pdf')。单击(函数(){
text=$(this.attr('text');

var first=“”您需要委派事件:

$(function () {
  $(document).on("click", '.pdf', function(){
    text = $(this).attr('text');
    var first = "'<object type=";
    var last = ' width="900" height="450"></object>';
    first = first.concat('"application/pdf" data=');
    first = first.concat(text);
    first = first.concat(last);
    first = first.concat("'");
    var pdfDiv = document.getElementById("contentDiv");
    pdfDiv.innerHTML=first;

    contentDivViewHelper();
    return false;
  });
});
$(函数(){
$(document).on(“click”,'.pdf',function(){
text=$(this.attr('text');

第一个变量=”“值得注意的是,委派应该发生在与已经存在的元素和新插入的元素最接近的父级。根据DOM结构的复杂性,
文档
级别的事件委派可能效率很低。也就是说,OP没有向您提供任何有关HTML结构的详细信息,因此此de这是一个很好的证明+1@War10ck谢谢,老兄。我本不该回答的,但为了让OP找到正确的解决方案,我回答了。谢谢,老兄。不用担心。这肯定是一个正确的回答。@War10ck再次感谢老兄。
:)
效果很好,谢谢!@j08691我相信你知道这样一个事实:拥有
6
声誉的人是不允许聊天的。是吗?@j08691需要
20
代表。
:)
<a id="recentnewslink" class="html" href="#" text="news.html">Current News/Announcements</a>
<a id="pdf" class="pdf" href="#" text="Announcements/PriceAnnouncement20151201.pdf">
$(function () {
  $(document).on("click", '.pdf', function(){
    text = $(this).attr('text');
    var first = "'<object type=";
    var last = ' width="900" height="450"></object>';
    first = first.concat('"application/pdf" data=');
    first = first.concat(text);
    first = first.concat(last);
    first = first.concat("'");
    var pdfDiv = document.getElementById("contentDiv");
    pdfDiv.innerHTML=first;

    contentDivViewHelper();
    return false;
  });
});