1
var count=0;
    function createOne() {
        var divTag = document.createElement("div");

动态创建的 div

        var br= document.createElement("br");
        count++;
        divTag.id = "div1"+count; 

ID 增量 +1

        divTag.setAttribute("align", "center");

        divTag.style.margin = "0px auto";
        divTag.style.width="430px";
        divTag.style.height="35px";
        divTag.style.background="olive";
        divTag.className = "dynamicDiv";

        divTag.innerHTML = "This HTML Div tag created "
                            + "using Javascript DOM dynamically.";

        document.body.appendChild(divTag);
           document.body.appendChild(br);
    }

> 需要使用 Jquery 保存在 php 中。

<body>
    <h1 align="center">
    Click it
    <input type="button" id="dev" onClick="createOne()" value="GET">
    </h1>
</body>
4

5 回答 5

1

** 使用 jQuery **

 // Within your createone() function 

 // Location of your PHP file
 var url = 'savemyvar.php';

 $.post(url, { div : divTag.id }).success(function(data, status, xhr)
 {
     // Do something on success
 }).error(function(data)
 {
     // Do something on error
 });

$.post 的信息 $.ajax 的辅助函数

$.ajax 文档 这会将您的 divTag 对象发送到您可以使用的 PHP 脚本

$_REQUEST['div'] 

访问。

于 2013-04-03T11:28:06.933 回答
1

在您的createOne()函数中,您可以通过您刚刚创建的元素的 ID 将 AJAX 回传到 PHP 脚本。

您可以在此处找到有关JQuery 的 AJAX 的更多信息

你还没有指定你想用这些信息做什么,或者什么时候这应该有助于开始。

于 2013-04-03T11:28:31.167 回答
1

我建议您通过 Ajax 发布数据

createOne = function() {
     var $div = $('#div1'+count);
     $.ajax({
        type: "POST",
        url: "some.php",
        data: { id: "div1"+count, html: $div.html() }
     }).done(function( msg ) {
        alert( "Data Saved: " + msg );
     });

} 
于 2013-04-03T11:33:10.773 回答
1

在 ajax 调用中,数据将如下所示:

var mydata = new Array ();
$("div[id^='div']").each (function (){
       mydata.push ({$(this).attr ("id") : $(this).text ()});
});

我使用 div 的文本作为值,但您可以根据需要更改它...

于 2013-04-03T11:36:14.960 回答
1

如果您使用的是 jQuery,请使用它。将您的函数转换为 jQuery 并使用 jQuery 的 ajax 函数。

JavaScript

jQuery(function($){

    $('#dev').click(function(){ createOne(); });

    window.count = 0;
    function createOne() {

        var new_id = 'div1' + (++count);

        $('body').append('<div class="dynamicDiv" id="' + new_id + '" style="margin: 0px auto; width: 430px; height: 35px; background-color: olive;">This HTML Div tag created using Javascript DOM dynamically.</div><br/>');    

        $.get('/div-id-saver.php', { 'id': new_id }, function(response){  
            console.log('post response:' + response);
        });

    }

});

HTML

<body>
    <h1>
    Click it
    <input type="button" id="dev" value="GET">
    </h1>
</body>

更多信息:http ://api.jquery.com/category/ajax/shorthand-methods/

于 2013-04-03T13:06:01.523 回答