简体   繁体   English

如何将e.target传递回服务器

[英]How do I pass e.target back to the server

I have a front-end where I track what element has been click ed and send it back to the server to do some work on the backend depending on what that element is. 我有一个前端,在其中跟踪click哪些元素, click将其发送回服务器,以便根据该元素是什么在后端进行一些工作。 The code is setup like so... 代码设置如下:

$('body').on('click', function(e) {
    $.post( '/edit', {el: $( e.target ).serialize()}, function(response) {
        console.log( response );
    });
});

But I get el as an empty string on the server. 但是我在服务器上将el作为空字符串获取。 What else can I do to get the e.target info to my server? 我还能怎么做才能将e.target信息发送到服务器?

Update: 更新:

I think my question could benefit from some context. 我认为我的问题可以从某些背景中受益。

The basic function of the app is to enable in-page editing. 该应用程序的基本功能是启用页内编辑。 A node server loads the HTML page I want to edit. 节点服务器将加载我要编辑的HTML页面。 Clicking on any element on this page lets me change the text in that element, which is then POST ed back to the node server, where I use the cheerio module to change the DOM representation and overwrite the original HTML file. 点击此页面上的任何元素让我改变该元素,然后将其文本POST编回节点服务器,在这里我用的是cheerio模块改变DOM表示并覆盖原来的HTML文件。 Reloading the page now gives me the new version of the page with the edits I had made. 现在,重新加载页面会为我提供该页面的新版本,其中包含我所做的修改。

But to apply the edits I have made on the front-end, cheerio needs the e.target to find the right element in its DOM representation and then change the text , since many of the elements on the page don't have id s. 但是要应用我在前端所做的编辑, cheerio需要e.target在其DOM表示中找到正确的元素,然后更改text ,因为页面上的许多元素都没有id

This is the whole app... 这是整个应用程序...

var
    express  = require( 'express' )
,   fs       = require( 'fs' )
,   cheerio  = require( 'cheerio' )
,   $        = ''
,   app      = express()
,   html     = ''
,   injected = "<script> \
                    $( 'body').on( 'click', function(e) {  \
                        $( e.target ).text( prompt('Enter new value:') );  \
                        $.post( '/edit', {el: $(e.target).serialize(), newVal: $(e.target).text()}, function(response) {  \
                            alert( response );  \
                        });  \
                    });  \
                </script>";

app.use( express.static(__dirname) )
app.use( express.bodyParser() )


app.get( '/', function( req, res ) {
    fs.readFile( process.argv[2], 'utf8', function(err, data) {
        $ = cheerio.load( data )
        err? console.log( err ): res.send( data.replace('</body>', injected + '</body>') )
    })
})

app.post( '/edit', function(req,res) {
    $( req.body.el ).text( req.body.newVal )
    fs.writeFile( process.argv[2], $.html(), function(err) {
        err? res.send( err ): res.send( 'file saved with changes!' )
    })
})

app.listen( 8080 )

I then run the app: 然后,我运行该应用程序:

node cms.js "E:\Dropbox\sites\index.html"

Theoretically, this should let me edit index.html "in-page" and without a code editor. 从理论上讲,这应该让我无需使用代码编辑器即可编辑index.html “页内”。 But getting the e.target back to the server intact remains the hurdle. 但是完整地将e.target返回服务器仍然是一个障碍。

Workaround: 解决方法:

My current workaround is to just POST the entire HTML of the page using $( 'html' ).html() so regardless of which element is clicked, I can get the new state of the page in it's entirety and overwrite the existing file with this new state. 我目前的解决办法是只POST使用整个页面的HTML $( 'html' ).html()所以无论点击哪个元素的,我可以得到网页的最新状态,在它的全文,并覆盖现有文件,这个新状态。 But I have browser extensions that inject their own HTML/JS and I want to avoid the painful process of stripping those away before saving to file. 但是我有一些浏览器扩展程序会注入自己的HTML / JS,并且我想避免在保存到文件之前将其剥离的痛苦过程。 For that, I need to tell cheerio exactly which element has been click ed. 为此,我需要准确地告诉cheerio click哪个元素。

Serialize works on forms and generates output similar to what your query string would be, single=Single&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio1 序列化在表单上工作并生成与查询字符串类似的输出, single=Single&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio1

You may want to check this answer , I use it here to get the html of the button when it's clicked: 您可能想检查一下这个答案 ,我在这里使用它来获得单击按钮时的html:

HTML 的HTML

<input id="b" type="button" value="click" /> 

JS JS

$("#b").click (function (e) {
    window.alert($(e.target).outerHTML());
});

jQuery.fn.outerHTML = function(s) {
    return s
        ? this.before(s).remove()
        : jQuery("<p>").append(this.eq(0).clone()).html();
};

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

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