admin管理员组文章数量:1129678
I am using this fancy little JavaScript to highlight a field as the user hovers over it. Could you please tell me if there is a way of adding an onclick
function which will act as a link and go to a URL?
<script>
$(function() {
$('tr').hover(function() {
$(this).css('background-color', '#eee');
$(this).contents('td').css({'border': '0px solid red', 'border-left': 'none', 'border-right': 'none'});
$(this).contents('td:first').css('border-left', '0px solid red');
$(this).contents('td:last').css('border-right', '0px solid red');
},
function() {
$(this).css('background-color', '#FFFFFF');
$(this).contents('td').css('border', 'none');
$('a#read_message.php').click(function(){ URL(); });
});
});
</script>
I am using this fancy little JavaScript to highlight a field as the user hovers over it. Could you please tell me if there is a way of adding an onclick
function which will act as a link and go to a URL?
<script>
$(function() {
$('tr').hover(function() {
$(this).css('background-color', '#eee');
$(this).contents('td').css({'border': '0px solid red', 'border-left': 'none', 'border-right': 'none'});
$(this).contents('td:first').css('border-left', '0px solid red');
$(this).contents('td:last').css('border-right', '0px solid red');
},
function() {
$(this).css('background-color', '#FFFFFF');
$(this).contents('td').css('border', 'none');
$('a#read_message.php').click(function(){ URL(); });
});
});
</script>
Share
Improve this question
edited Apr 16, 2019 at 5:06
Kamil Kiełczewski
92.2k34 gold badges394 silver badges370 bronze badges
asked Oct 25, 2012 at 15:26
John TaylorJohn Taylor
1,4972 gold badges11 silver badges15 bronze badges
2
- How do I get the url value, is it a link in one of the table cells or does they all go to the same url – MagePal Extensions Commented Oct 25, 2012 at 15:29
- 3 You have a rather strange id there '#read_message.php'? – Alex Gill Commented Oct 25, 2012 at 15:41
9 Answers
Reset to default 244Try
window.location = url;
Also use
window.open(url);
if you want to open in a new window.
Simply use this
onclick="location.href='pageurl.html';"
In jquery to send a user to a different URL you can do it like this:
$("a#thing_to_click").on('click', function(){
window.location = "http://www.google.com/";
});
this way will work too but the above is the newer more correct way to do it these days
$("a#thing_to_click").click(function(e){
e.preventDefault();
window.location = "http://www.google.com/";
});
function URL() {
location.href = 'http://your.url.here';
}
HTML
<input type="button" value="My Button"
onclick="location.href = 'https://myurl'" />
MVC
<input type="button" value="My Button"
onclick="location.href='@Url.Action("MyAction", "MyController", new { id = 1 })'" />
If you would like to open link in a new tab, you can:
$("a#thing_to_click").on('click',function(){
window.open('https://yoururl.com', '_blank');
});
In case you're dealing with <a>
tag, and you want to interrupt going to the default href
you should use this
instead.
Go to default url (yahoo):
<a href="https://yahoo.com" onclick="location.href='https://google.com';">
Go to new url (google) onclick
:
<a href="https://yahoo.com" onclick="this.href='https://google.com';">
By using this
you're interrupting the current browser onclick
event and changing href
before continuing to default behaviour of <a href='...
Not completely sure I understand the question, but do you mean something like this?
$('#something').click(function() {
document.location = 'http://somewhere.com/';
} );
try
location = url;
function url() {
location = 'https://example.com';
}
<input type="button" value="Inline"
onclick="location='https://example.com'" />
<input type="button" value="URL()"
onclick="url()" />
本文标签: jqueryAdding an onclick function to go to url in JavaScriptStack Overflow
版权声明:本文标题:jquery - Adding an onclick function to go to url in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736748922a1950931.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论