我有一个while循环,其中创建了多个包含信息的表。每个表中都有一个facebook和twitter图标。读者可以通过onclick事件分享或推文表内的信息。
下面是每个事件。
<?php
echo'<script type="text/javascript">
function tweet(){
var sharer = "https://twitter.com/intent/tweet?source=webclient&text=',$title2,' - http://www.globatum.com/lite/post.php?id=',$row['id'],'";
window.open(sharer,''sharer'' , ''width=600,height=500'');}
</script>';
?>
<?php
echo'<script type="text/javascript">
function fbshare(){
var share = "https://www.facebook.com/sharer/sharer.php?u=http://www.globatum.com/lite/post.php?id=',$row['id'],'";
window.open(share,''share'' , ''width=600,height=500'');}
</script>';
?>
奇怪的是它居然有效。有点。当我点击fb图标时,会弹出一个带有对话框的小窗口。twitter也是如此。问题是,当我看的标题弹出…它使用来自所有表的相同信息。
What I want…
表1 fb图标点击应该打开窗口https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php ? id = 1
表2将是https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php ? id = 2
表3 ..https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=3等等
但是当我点击图标…
表1:打开窗口https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php ? id = 1(好)
表2:打开窗口…https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php ? id = 1(坏的管理者)
表3:打开窗口…https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php ? id = 1(坏的管理者)
这听起来像一个简单的sql问题,但当我拉起页面源我得到
表1
<script type="text/javascript">
function tweet(){
var sharer = "https://twitter.com/intent/tweet?source=webclient&text=random title - http://www.makeupsite.com/post.php?id=39";
window.open(sharer,'sharer' , 'width=600,height=500');}
</script><script type="text/javascript">
function fbshare(){
var share = "https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=39";
window.open(share,'share' , 'width=600,height=500');}
</script>
表2 <script type="text/javascript">
function tweet(){
var sharer = "https://twitter.com/intent/tweet?source=webclient&text=random title - http://www.makeupsite.com/post.php?id=43";
window.open(sharer,'sharer' , 'width=600,height=500');}
</script><script type="text/javascript">
function fbshare(){
var share = "https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=43";
window.open(share,'share' , 'width=600,height=500');}
</script>
基本上页面源说的是一件事…但是窗户。Open做的是完全不同的事情。
你在一遍又一遍地重新定义同一个函数。根据浏览器的不同,您将始终调用您定义的tweet()或fbshare()的第一个或最后一个实例。
在文档的页脚设置一个函数:
function shareLink(targetURL){
window.open(targetURL, 'share', 'width=600,height=500');
}
然后,将其绑定到您的社交按钮:
onclick=shareLink("[twitter URL here]");
或
onclick=shareLink("[facebook URL here]");
明白我的意思了吗?EDIT哎呀,如果我把名字写对了会有帮助