开发者

jQuery Click how to?

I have a newsletter I am sending and when a certain link is clicked I want it to show informat开发者_如何学编程ion not shown on the page by default. I am doing this by having the URL check for URL parameters but I am doing something wrong as it does not work. Can someone help me please since I am not very good at javascript.

Here is my code:

<p class="textcenter"><a id="click" href="javascript:showPhotoInfo();">Photo Info
</a></p>
<div id="PhotoInfo" class="hide">
<p>Info Here...</p>
</div>

<script type="text/javascript">
var $j = jQuery.noConflict();
$j(document).ready(function() {
var url = window.location.href;
url = url.toLowerCase();
if (url.indexOf('?photoinfo=1') != -1 || url.indexOf('&photoinfo=1') != -1) {
    $j("#click").click();
}
});
function showPhotoInfo() {
        document.getElementById('PhotoInfo').style.display='block';
    }
</script>

I have also tried this:

<script type="text/javascript">
var $j = jQuery.noConflict();
$j(document).ready(function() {
var url = window.location.href;
url = url.toLowerCase();
if (url.indexOf('?photoinfo=1') != -1 || url.indexOf('&photoinfo=1') != -1) {
        function showPhotoInfo() {
        document.getElementById('PhotoInfo').style.display='block';
    }
}
});
function showPhotoInfo() {
        document.getElementById('PhotoInfo').style.display='block';
    }
</script>

If a person clicks the link on the page it shows correctly. I need the URL parameter to be checked then click the link to display the needed information.


I meant this:

var $j = jQuery.noConflict();
$j(document).ready(function() {
   var url = window.location.href;
   url = url.toLowerCase();
   if (url.indexOf('photoinfo=1') != -1) {
      showPhotoInfo();
   }
});

function showPhotoInfo() {
    document.getElementById('PhotoInfo').style.display='block';
}


Instead of having <a id="click" href="javascript:showPhotoInfo();">Photo Info</a> try doing the following (on documnet ready). It will "listen" for click on the link and will not follow the link when clicked (you will stay on the page).

<a id="click" href="#">Photo Info</a>
$j('#click').click(function(){
    $j('#PhotoInfo').show();
    return false;
});

Also trigger the click by $j("#click").trigger('click'); - imho it's more clear.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜