请问调用JS 点击SPAN 代码该怎么写请教
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请问调用JS 点击SPAN 代码该怎么写请教相关的知识,希望对你有一定的参考价值。
代码是:<SPAN class=publish>发布</SPAN>
请问调用js以 发布 为基准 点击 ,该怎么写?
请问调用js以 class=publish 为基准 点击 ,该怎么写?
var spans = document.querySelectorAll("span");
for(var i =0;i<spans.length;i++)
var si = spans[i];
if(si.innerhtml == "发布")
si.onclick = function()
// TODO
// 以 class=publish 为基准
var spans = document.querySelectorAll("span.publish");
for(var i = 0;i<spans.length;i++)
spans[i].onclick=function()
// TODO
追问
不好意思 您的代码测试不成功,好像一定要 javascript 代码才能调用,我把他们改成一行后,把您的代码放进去后没反应
追答是不是你添加的时候出了什么差错呢,你贴出你的代码来
追问把 .onclick = function() // TODO 改成 .click(function() alert(1) ) 成功了
本回答被提问者采纳 参考技术B 主动触发事件是一个经常会用到的技巧,它很重要。它和事件绑定一样存在着浏览器兼容性问题。传统浏览器(IE8-)使用createEventObject来创建事件对象,用fireEvent这个方法来触发事件;现代浏览器用createEvent来创建对象,用dspatchEvent来触发事件。//获取浏览器版本
var isIE=navigator.userAgent.match(/MSIE (\d)/i);
isIE=isIE?isIE[1]:undefined;
//事件函数
function onclick(e)
e=e||event;
alert(e.msg||"表酱紫啦,人家羞涩啦~");
;
//给document绑定一个点击事件
isIE<9
//传统浏览器使用attachEvent
?document.attachEvent("onclick",onclick)
//现代浏览器使用addEventListner
:document.addEventListener("click",onclick,false);
//触发自定义事件
if(isIE<9)
//传统浏览器
//创建对象
var event=document.createEventObject();
//给事件对象添加属性
event.msg="我是fireEvent触发的";
//触发事件
document.fireEvent("onclick",event);
else
//现代浏览器
//创建事件对象
var e=document.createEvent("MouseEvents");
//初始化事件对象
e.initMouseEvent("click"),
//给事件对象添加属性
e.msg="我是despatchEvent触发的";
//触发事件
document.dispatchEvent(e);
; 参考技术C
首先你html写错了,规范如下:
<span class="publish"></span>jquery如下:
$(".publish").click(function()
alert(1)
)
)追问
不好意思 您的代码测试不成功,好像一定要 javascript 代码才能调用,不知道能不能改?
已经结合您的代码,和另一位朋友的代码测试成功。谢谢您的回答
谁能帮写一个点击链接自动弹出提示窗口的js代码?
我想在 Discuz! 5.0.0论坛程序中加入一段js代码,让会员点击下载附件前自动弹出提示窗口,请问怎么写?
你需要修改attachment.php文件 在根目录下在$attachexists = $ispaid = FALSE;下面添加
if (!empty($checktwo))
在最末尾的?>前添加
else
echo "<script>alert('写上你要提示的内容')</script>";
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=$PHP_SELF?checktwo=1&aid=$aid\">";
我在本机架设Discuz! 5.0.1调试通过了
完整代码如下:
<?php
/*
[Discuz!] (C)2001-2006 Comsenz Inc.
This is NOT a freeware, use is subject to license terms
$RCSfile: attachment.php,v $
$Revision: 1.13 $
$Date: 2006/08/11 06:51:07 $
*/
require_once './include/common.inc.php';
$discuz_action = 14;
if($attachrefcheck && $_SERVER['HTTP_REFERER'] && preg_replace("/https?:\/\/([^\/]+).*/i", "\\1", $_SERVER['HTTP_REFERER']) != $_SERVER['HTTP_HOST'])
//header("Location: $boardurlimages/common/invalidreferer.gif");
showmessage('attachment_referer_invalid', NULL, 'HALTED');
/*
$query = $db->query("SELECT a.*, t.fid, p.authorid FROM $tablepreattachments a, $tableprethreads t, $tablepreposts p
WHERE a.aid='$aid' AND t.tid=a.tid AND p.pid=a.pid AND t.displayorder>='0' AND p.invisible='0'");
$attach = $db->fetch_array($query);
*/
periodscheck('attachbanperiods');
$attachexists = $ispaid = FALSE;
if (!empty($checktwo))
if(!empty($aid))
$query = $db->query("SELECT * FROM $tablepreattachments WHERE aid='$aid'");
if($attach = $db->fetch_array($query))
$query = $db->query("SELECT tid, fid, price, special FROM $tableprethreads WHERE tid='$attach[tid]' AND displayorder>='0'");
$thread = $db->fetch_array($query);
if($thread['fid'])
$query = $db->query("SELECT authorid FROM $tablepreposts WHERE pid='$attach[pid]' AND invisible='0'");
if($db->num_rows($query))
$attach['authorid'] = $db->result($query, 0);
$attachexists = TRUE;
if($allowgetattach && ($attach['readperm'] && $attach['readperm'] > $readaccess) && $adminid <= 0 && !($discuz_uid && $discuz_uid == $attach['authorid']))
showmessage('attachment_forum_nopermission', NULL, 'NOPERM');
if(!$thread['special'] && $thread['price'] > 0 && (!$discuz_uid || ($discuz_uid && $discuz_uid != $attach['authorid'] && $adminid <=0)))
$query = $db->query("SELECT uid FROM $tableprepaymentlog WHERE uid='$discuz_uid' AND tid='$attach[tid]'");
if($db->result($query, 0))
$ispaid = TRUE;
else
showmessage('attachment_payto', 'viewthread.php?tid='.$attach['tid']);
$filename = $attachdir.'/'.$attach['attachment'];
if(is_readable($filename) && $attachexists)
$query = $db->query("SELECT f.viewperm, f.getattachperm, f.getattachcredits, a.allowgetattach FROM $tablepreforumfields f
LEFT JOIN $tablepreaccess a ON a.uid='$discuz_uid' AND a.fid=f.fid
WHERE f.fid='$thread[fid]'");
$forum = $db->fetch_array($query);
if(!$ispaid)
if(!$forum['allowgetattach'])
if(!$forum['getattachperm'] && !$allowgetattach)
showmessage('group_nopermission', NULL, 'NOPERM');
elseif(($forum['getattachperm'] && !forumperm($forum['getattachperm'])) || ($forum['viewperm'] && !forumperm($forum['viewperm'])))
showmessage('attachment_forum_nopermission', NULL, 'NOPERM');
if(!($isimage = preg_match("/^image\/.+/", $attach['filetype'])))
checklowerlimit($creditspolicy['getattach'], -1);
if(empty($noupdate))
if($delayviewcount == 2 || $delayviewcount == 3)
$logfile = './forumdata/cache/cache_attachviews.log';
if(substr($timestamp, -1) == '0')
require_once DISCUZ_ROOT.'./include/misc.func.php';
updateviews('attachments', 'aid', 'downloads', $logfile);
if(@$fp = fopen(DISCUZ_ROOT.$logfile, 'a'))
fwrite($fp, "$aid\n");
fclose($fp);
elseif($adminid == 1)
showmessage('view_log_invalid');
else
$db->query("UPDATE $tablepreattachments SET downloads=downloads+'1' WHERE aid='$aid'", 'UNBUFFERED');
if(!$isimage)
$forum['getattachcredits'] = $forum['getattachcredits'] ? unserialize($forum['getattachcredits']) : array();
$getattachcredits = $forum['getattachcredits'] ? $forum['getattachcredits'] : $creditspolicy['getattach'];
updatecredits($discuz_uid, $getattachcredits, -1);
$filesize = filesize($filename);
ob_end_clean();
header('Cache-control: max-age=31536000');
header('Expires: '.gmdate('D, d M Y H:i:s', $timestamp + 31536000).' GMT');
header('Content-Encoding: none');
$attach['filename'] = (strtolower($charset) == 'utf-8' && strexists($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ? urlencode($attach['filename']) : $attach['filename'];
if($isimage && !empty($noupdate))
header('Content-Disposition: inline; filename='.$attach['filename']);
else
header('Content-Disposition: attachment; filename='.$attach['filename']);
header('Content-Type: '.$attach['filetype']);
@$fp = fopen($filename, 'rb');
@flock($fp, 2);
$attachment = @fread($fp, $filesize);
@fclose($fp);
echo $attachment;
else
showmessage('attachment_nonexistence');
else
echo "<script>alert('$PHP_SELF')</script>";
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=$PHP_SELF?checktwo=1&aid=$aid\">";
?>
还有不明白的给我站内留言 enjoy it! 参考技术A 没研究过Discuz!你要先找到添加附件的页面,然后小做修改.添加附件的同时添加上你的弹出窗口.
以上是关于请问调用JS 点击SPAN 代码该怎么写请教的主要内容,如果未能解决你的问题,请参考以下文章