简体   繁体   English

使用原型js,如何在用户单击链接时打开对话框窗口并显示动态内容

[英]Using prototype js, how to open dialog window and display dynamic content when a user clicks link

I have a table with several rows and columns. 我有一个有几行和几列的表。 The user clicks on given text and dialog should open with dynamic content. 用户单击给定的文本,将打开包含动态内容的对话框。

eg 例如

<tr> 
  <td>
    <strong>item1</strong>
  </td>
</tr>
<tr> 
 <td>
    <strong>item2</strong>
 </td>
</tr>

The user clicks on item1 or item2 and a javascript window is open with relevant text. 用户单击item1或item2,将打开一个带有相关文本的javascript窗口。

I can do this using prototypejs and window.js which seems to work, but doesnt look like clean javascript. 我可以使用似乎可以正常工作的prototypejs和window.js来执行此操作,但看起来不像干净的javascript。

<td>
  <a href='javascript:openModalDialog("<?php echo item1Id?>")'><strong>item1</strong></a>
</td>

<td>
 <a href='javascript:openModalDialog("<?php echo item2Id?>")'><strong>item2</strong></a>
</td>

I want to use unobstructive javascript using event listeners 我想通过事件监听器使用通俗易懂的javascript

$$('.myClass').invoke('observe', 'click', function(event){
    openModalDialog("dynamic content")  ;   
});


<span class="myClass"><span style="display:none"><?php echo item1Id?></span>item1</span>

<span class="myClass"><span style="display:none"><?php echo item2Id?></span>item2</span>

I dont want to display item1Id, item2Id, has PHP will replace it with dynamic content which needs to be displayed in the popup window when the user clicks item1 or item2. 我不想显示item1Id,item2Id,PHP将其替换为动态内容,当用户单击item1或item2时,该动态内容需要在弹出窗口中显示。

Am I on the right tracks? 我在正确的轨道上吗?

How to access item1Id, or item2Id content within the onclick and pass it to openModalDialog? 如何在onclick中访问item1Id或item2Id内容并将其传递给openModalDialog?


I'm not too sure span inside another span is the best way but only way i can think of it working. 我不太确定另一个跨度内的跨度是最好的方法,但是我认为它起作用的唯一方法。

What I need is how to access the dynamic content inside the handler. 我需要的是如何访问处理程序中的动态内容。

For example 例如

On page load 页面加载

<span class="myClass"><span style="display:none"><?php echo item1Id?></span>item1</span>
<span class="myClass"><span style="display:none"><?php echo item2Id?></span>item2</span>

may become something like 可能会变成

<span class="myClass"><span style="display:none">This is dynamic content generated by php </span>item1</span>
<span class="myClass"><span style="display:none">some more different content </span>item2</span>

So in the handler when the user click item1 I need the content "This is dynamic content generated by php" so I can pass it to openModalDialog function. 因此,在处理程序中,当用户单击item1时,我需要内容“这是由php生成的动态内容”,因此我可以将其传递给openModalDialog函数。 How can I access the content, this is different for each row. 我如何访问内容,每一行都不同。

Any ideas? 有任何想法吗?

When I do console.log(this) i get 当我做console.log(this)时

To me it seems you are on the right track. 对我来说,您似乎走对了。 As for how to access the item1Id or item2Id, you can see this demo: http://jsfiddle.net/yg7XX/ 至于如何访问item1Id或item2Id,您可以看到此演示: http : //jsfiddle.net/yg7XX/

I've commented it nicely, hoping it might help. 我已经很好地评论了它,希望对您有所帮助。 Btw, I haven't worked with prototype ever before. 顺便说一句,我以前从未使用过原型。 My solution was just a guess resulting due to working with jQuery, and the guess worked! 我的解决方案只是由于使用jQuery而产生的猜测,而这种猜测有效! :) :)

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

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