简体   繁体   English

在模式onclick中显示锚标记标题属性的Javascript

[英]Javascript to show anchor tag title attribute in modal onclick

Just wondering if you can point me in the right direction here. 只是想知道您是否可以在这里为我指明正确的方向。 I have this PHP code and want to be able to use onclick on dynamically created links to display the link's title in a modal window with close option. 我有此PHP代码,并希望能够在动态创建的链接上使用onclick,以在带关闭选项的模式窗口中显示链接的标题。 Sort of like a tooltip. 有点像工具提示。 Don't want to use the native browser tooltip. 不想使用本机浏览器工具提示。 Am assuming Javascript would be best? 假设Java脚本是最好的吗? Any pointers would be very much appreciated. 任何指针将不胜感激。

while ($row = $result->fetch_assoc())
{ ?> 
<a style="font-size:16px;" ID="<? echo $row['ID'] ?>" title="<? 
echo  $row ['title']  ?> 
by
<? echo $row ['author'] ?>"><span title=""><? echo $row ['first_line'] ?>
</span></a>

<?
echo "&nbsp"; 
if ($count >= 6) 
{ 
echo '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'; 
$count = 0; 
} else { 
$count++; } 

}

Add a class to the links (say, 'alertTitle'). 在链接中添加一个类(例如,“ alertTitle”)。 Then, here's the JS that could help you, if I get the question right: 然后,如果我的问题正确的话,这是可以帮助您的JS:

var els = document.getElementsByClassName('alertTitle');
// Add event listener to each
for (var i = 0; i < els.length; i++){
  els[i].onclick = function(){
  window.alert(this.title); // You could do anything with this.title
}

Here's the working example: JSFiddle 这是工作示例: JSFiddle

您可以使用bootstrap或其他css / js框架显示模式并自定义模式显示方法,如下所示:

`http://jsfiddle.net/saeedsalehi/1aeur58f/665/`

Use the event listener $('a').on() : 使用事件监听器$('a').on()

 var modal = $('#my-modal'); $('a').on('click', function() { var title = $(this).attr('title'); modal.find('.modal-title').text(title); $('#my-modal').modal('show'); }); 
 <script src="//code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script> <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" /> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script> <a href="#1" id="1" title="Title 1">Link 1</a><br/> <a href="#2" id="2" title="Title 2">Link 2</a><br/> <a href="#3" id="3" title="Title 3">Link 3</a><br/> <div id="my-modal" class="modal fade" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title"></h4> </div> <div class="modal-body"> <p>Some text in the modal.</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> 

View on JSFiddle 在JSFiddle上查看

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

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