Javascript在modal onclick中显示锚标签title属性

时间:2017-09-19 15:21:50

标签: javascript php css

只是想知道你是否可以在这里指出我正确的方向。我有这个PHP代码,并希望能够使用动态创建的链接onclick在带有关闭选项的模态窗口中显示链接的标题。有点像工具提示。不想使用本机浏览器工具提示。假设Javascript最好吗?任何指针都将非常感激。

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++; } 

}

3 个答案:

答案 0 :(得分:1)

在链接中添加一个类(例如,'alertTitle')。然后,如果我得到正确的答案,这里的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
}

以下是工作示例:JSFiddle

答案 1 :(得分:0)

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

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

答案 2 :(得分:-1)

使用事件监听器$('a').on()

&#13;
&#13;
var modal = $('#my-modal');

$('a').on('click', function() {
  var title = $(this).attr('title');
  modal.find('.modal-title').text(title);
  $('#my-modal').modal('show');
});
&#13;
<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>
&#13;
&#13;
&#13;

View on JSFiddle