Concept: Focus the div when user click the Comment link. There was two link in my php page. one is comment link
and another one is readmore link
. The two link goes to the same page but i want to focus the div element
when click c开发者_Python百科omment link .
It sounds like you want to scroll to the div element, and not actually focus it. If so, then:
<a href="#id_of_div">comments</a>
If you do want to focus it then (after thinking very hard about whether you should use a more conventional user control, such as a button, instead):
- Give the div a tabindex attribute
- Call the focus method of the div
e.g.
<div id="foo" tabindex="0"> … </div>
document.getElementById('foo').focus();
If they both link to a page called more.php
for example, the 'readmore' link could simply have the link:
href="more.php"
Whereas the 'comment' link could have something like:
href="more.php?comment=1#commentbox"
The comment
querystring could then be used like:
$(document).ready(function(){
if (getParameterByName("comment") == 1) {
$(".commentbox").focus();
}
});
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
The #comment
part of the URL is purely for anchor purposes to position the user on the correct part of the page.
<div id="commentContend" tabindex="0" contenteditable="true"
style="width:300px;height:200px;border:1px solid black;"></div>
<a href="#"
onclick="document.getElementById('commentContend').focus();return false;">
Comment</a>
so simply use $('selector').focus()
This would work great with inputs or textareas
You can create a url like: http://mydomain.com/mypage.php#mysection, where #mysection is the section on the page you want to focus.
Before your section add an anchor like <a name="mysection"/>
. Obviously you'll have one for "comment" and one for "readmore".
精彩评论