简体   繁体   English

页面加载后应用Jquery

[英]Apply Jquery After Page Loads

I have following HTML code to add new div: 我有以下HTML代码来添加新的div:

 <a class="add-link" id="buttonAdd" href="@Url.Action("Foo", "Foo1", new {actiontype = "Add" })">Add New Openings</a>

When i click on Add , new div is added. 当我单击“添加”时,将添加新的div。 I want to show that div in editable mode by default, but as there is link in html for <a> I am not able to accomplish it. 我想在默认情况下以可编辑模式显示div,但是由于<a> html中有链接,所以我无法实现它。 I want to show first div that is added as editable . 我想显示添加为editable的第一个div。

$(document).ready(function () {
    $('.container').on('click', '.add-link', function () {
            $("#accordion").first().find('.panel-title, .panel-collapse').attr('contenteditable', true).css('border', '2px solid');
            $('#accordion').first().find('.edit-link').css('display', 'none');
            $('#accordion').first().find('.done-link').css('display', 'inline');
        });

Can someone guide me please. 有人可以指导我吗?

You need to prevent the default click behaviour: 您需要阻止默认的点击行为:

$(document).ready(function () {
    $('.container').on('click', '.add-link', function (e) {
            e.preventDefault();
            $("#accordion").first().find('.panel-title, .panel-collapse').attr('contenteditable', true).css('border', '2px solid');
            $('#accordion').first().find('.edit-link').css('display', 'none');
            $('#accordion').first().find('.done-link').css('display', 'inline');
        });

Apparently this is only the first issue. 显然,这只是第一个问题。 They also want the code to run after the link page has been loaded!!! 他们还希望代码在链接页面加载后运行!!! :) :)

How web pages work. 网页的工作方式。

Because the web was designed to be stateless, to a web browser, each page load is separate to the last. 由于Web被设计为无状态的,因此对于Web浏览器来说,每个页面加载都独立于最后一个页面。 Even if they are the same URL. 即使它们是相同的URL。 The practical upshot of this is that your code gets reloaded and restarted on each page. 实际的结果是在每个页面上重新加载并重新启动代码。

Based on your comments you want to add a div then apply jQuery to the accordion. 根据您的评论,您想要添加一个div,然后将jQuery应用于手风琴。 You have two options. 您有两个选择。

a) 一种)

Detect the change of divs on page load and apply the change then. 检测页面加载时div的更改,然后应用更改。

b) b)

Run the link via an Ajax call and update just the required part of the page using informtion in the new page returned from the server. 通过Ajax调用运行链接,并使用从服务器返回的新页面中的信息仅更新页面的所需部分。

Of these two options only the Ajax solution will allow you to retain code-control on the originating page. 在这两个选项中,只有Ajax解决方案允许您将代码控制保留在原始页面上。 I would suggest the URL be changed to a method which returns a PartialPage containing just the new DIV. 我建议将URL更改为一种返回仅包含新DIV的PartialPage的方法。

In order to answer this definitively, we need more information about how you prefer to solve the problem. 为了确定性地回答此问题,我们需要有关您希望如何解决该问题的更多信息。

If you go Ajax: 如果您使用Ajax:

If you go with an Ajax update, I suggest you change the link to an action that just returns the new DIV in a PartialView. 如果要进行Ajax更新,建议您将链接更改为仅在PartialView中返回新DIV的操作。 Then you can do something like this: 然后,您可以执行以下操作:

$('.container').on('click', '.add-link', function (e) {
    // Stop default processing of the link
    e.preventDefault();
    // Load the new page (specified by the clicked link's href)
    var $link = $(this);
    var url = $link.attr('href');
    alert(url); // for testing - remove this

    // Execute a HTML GET command on the URL
    $.get(url, function (html) {
        // Add the new DIV "somewhere" to be determined
        $('.container').append(html);

        // Then do your other processing
        $("#accordion").first().find('.panel-title, .panel-collapse').attr('contenteditable', true).css('border', '2px solid');
        $('#accordion').first().find('.edit-link').css('display', 'none');
        $('#accordion').first().find('.done-link').css('display', 'inline');
    });
});

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

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