将内容推送到浏览器…改变DOM?

huangapple go评论122阅读模式
英文:

Push content to a the browser...Change the DOM?

问题

好的,以下是翻译好的内容:

好的。我有一个我一直在努力的激情项目。

我现在处于这样一个阶段,我有一个移动应用和一个后端,实际上我使用一个托管的消息传递/云处理服务作为我的新后端,取代了我自己的服务器。

问题在于我不懂JavaScript。

我知道如何从移动端到后端,以及其他一切好的东西......现在我想做的就是简单地改变一个DOM元素,无论是使用GO代码还是Python代码。

移动端 ----> 消息传递/处理 ----> 浏览器/DOM

基本上,将一个URL或一段HTML代码推送到浏览器中以便显示。

对于这个问题,我了解到我的唯一选择是JavaScript。

我看过Pusher和WebSockets......

但我仍然不明白如何实际改变HTML元素。

英文:

Ok. I have this passion project I've been working on.

I'm at the stage where I have a mobile app and a backend,
really I use a hosted messaging/cloud processing service as my NEW
backend, replacing my own servers.

The problem lies in my non ability to understand javascript.

I know how to go from mobile to backend and all that good stuff...
What i now what to do is simply change a DOM element.
From either GO code or Python code.

mobile ----> messaging/processing ----> browser/DOM

Basically push either a url or a string of html code so that it displays
in the browser

For this I understand my only choice is javascript.

I've looked at pusher and web sockets...

but i still dont understand how to ACTUALLY make the html element change.

答案1

得分: 3

由于您已经考虑了Pusher,您可以从Pusher获取必要的密钥。一旦您获得了访问权限,在客户端中,您可以将Pusher js库添加到要向其“推送”事件的HTML文件中。

然后,您需要订阅一个频道(js)。

var channelName = "my-channel"
var channel = pusher.subscribe(channelName);

一旦您从客户端订阅了一个频道。使用python pusher服务器库(python)触发事件并附带您需要发送的数据:

push = pusher.Pusher(app_id='your-pusher-app-id', key='your-pusher-key', secret='your-pusher-secret')
push['my-channel'].trigger('dom-change-event', {'data': 'data'})

您可以从客户端侧(js)监听此事件:

channel.bind("dom-change-event", function(data){ // the data you Pushed from backed
  $(".div-to-change").hide(); // with the help of jQuery select the dom, and hide it.
})

Pusher与客户端维持着一个WebSocket连接,一旦服务器触发了一个事件,Pusher会在客户端引发该事件。

DOM操作(更改):

您所提到的DOM更改可以通过多种方式完成。以下是其中一些方法:

  1. 在前端准备好所有内容(HTML代码),并根据服务器的推送消息显示/隐藏DOM的不同部分,就像上面提到的那样。
  2. 根据Pusher消息使用JavaScript动态构建DOM。
  3. 从服务器发送HTML代码,然后将其附加到DOM中。

这里需要注意的重要一点是,在上面的示例中,DOM操作使用了一个名为jQuery的库。jQuery使得DOM操作变得简单。http://jquery.com/

还可以参考这篇文章,以了解如何使用jQuery开始进行DOM操作(更改)。

英文:

Since you have considered Pusher, you can get the necessary keys from Pusher. Once you have access to that, in the client, you can add the Pusher js library to the html file you want to "push" events to.

Then you need to subscribe to a channel(js).

var channelName = "my-channel"
var channel = pusher.subscribe(channelName);

Once you subscribe to a channel from client. Trigger an event along with the data you need to send using python pusher server lib (python):

push = pusher.Pusher(app_id='your-pusher-app-id', key='your-pusher-key', secret='your-pusher-secret')
push['my-channel'].trigger('dom-change-event', {'data': 'data'})

You can listen to this event from your client side(js):

channel.bind("dom-change-event", function(data){ // the data you Pushed from backed
  $(".div-to-change").hide(); // with the help of jQuery select the dom, and hide it.
})

Pusher maintains a websocket connection with the client, and as soon as an event is triggered from the server, it is raised by Pusher at the client side.

DOM manipulation(change):

The DOM change that you were referring to can be done in many ways. Some are mentioned below:

  1. Have all the Content(html code) ready in your frontend and based on the server's push message you can show/hide different parts of your DOM like the one mentioned above.
  2. Construct the DOM on the fly with javascript based on the pusher message
  3. Send the HTML code from the server and simply append it to the DOM.

IMPORTANT thing to note here is that DOM manipulation is done using a library called jQuery in the above example. jQuery makes it easy for you do DOM manipulations. http://jquery.com/

Also refer to this article to get started on doing DOM manipulations(changes) using jQuery.

答案2

得分: 0

或许你可以使用反向AJAX,我使用DWR框架
http://directwebremoting.org/dwr/documentation/reverse-ajax/index.html

英文:

Maybe you can use reverse AJAX, I use DWR Framework
http://directwebremoting.org/dwr/documentation/reverse-ajax/index.html

答案3

得分: -2

JavaScript改变元素的代码示例:

HTML

<div id="myDiv" class="divvy">一些内容</div>

JavaScript

// 选择元素
var elementById = document.getElementById("myDiv");     // 通过id返回元素
var elements = document.getElementsByClassName("divvy"); // 返回具有该类的所有元素的数组

参考资料

英文:

JavaScript to change an element

HTML

&lt;div id=&quot;myDiv&quot; class=&quot;divvy&quot;&gt;some content&lt;/div&gt;

JavaScript

// selecting elements
var elementById = document.getElementById(&quot;myDiv&quot;);     // returns the element by id
var elements = document.getElementByClassName(&quot;divvy&quot;); // returns array of all elements with this class

References

huangapple
  • 本文由 发表于 2013年12月5日 16:11:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/20394667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定