点击超链接禁止跳到顶部解决办法

本文介绍了解决点击超链接导致页面跳转至顶部的问题。提供了两种方法:一是修改超链接的href属性为'###';二是将href设置为'javascript:void(0)'。这两种方法都能有效阻止页面跳转行为。

今天豆芽碰到一个问题。我在使用ajax实现与服务器异步传输的时候需要点击一个超链接。但是这个超链接位于网页底部,所以当我每次点击超链接的时候虽然实现了与服务器的通信。但是,页面一下子跳转到了顶部,感觉不是很好。下面是豆芽的解决办法。

1、一般我使用超链接是<a href="#">点击超链接禁止跳到顶部解决办法</a>,如果你的也是这样的话,可以将 href="#" 改为 href="###" ,试试,是不是就可以啦。还有一种办法。

2、链接的形式是更上面的一样的。但是我们将改为:href="javascript:void(0)" 也同样可以解决这个问题。

 

一点点分享,写完收工。呵呵

 

 

文章出自:http://www.aichengxu.com/view/514

修改代码,当通过其他sheet的超链接回来该sheet时,要更改原本的显示比例,如本来此sheet的比例是150%,当我从超链接跳转回来该sheet时显示比例应该保持150%Option Explicit Private TextBoxUpdateInProgress As Boolean ' ==== ??????? ==== Dim currentMatches As Collection ' ????????? Dim currentMatchIndex As Long ' ????????? Dim originalZoom As Integer ' ?????? ' ==== ????????? ==== Private Sub TextBox1_Change() Static lastSearch As Single If Timer - lastSearch < 0.3 Then Exit Sub ' 300ms?? lastSearch = Timer Dim searchVal As String: searchVal = Trim(Me.TextBox1.Text) ' ?????? Set currentMatches = New Collection currentMatchIndex = -1 ' ????=3????? If Len(searchVal) < 3 Then Exit Sub Dim arrColumns: arrColumns = Array("A", "C", "D", "F") Dim found As Boolean: found = False application.ScreenUpdating = False application.Calculation = xlCalculationManual application.EnableEvents = False ' ?????????? Dim col As Variant For Each col In arrColumns Dim lastRow As Long lastRow = Cells(Rows.Count, col).End(xlUp).Row If lastRow < 3 Then lastRow = 3 Dim searchRange As Range Set searchRange = Range(col & "3:" & col & lastRow) Dim dataArr As Variant dataArr = searchRange.Value Dim i As Long For i = 1 To UBound(dataArr, 1) If InStr(1, CStr(dataArr(i, 1)), searchVal, vbTextCompare) > 0 Then currentMatches.Add searchRange.Cells(i, 1) found = True End If Next i Next col ' ???????? If currentMatches.Count > 0 Then currentMatchIndex = 0 application.Goto currentMatches(1), True End If CleanExit: application.Calculation = xlCalculationAutomatic application.EnableEvents = True application.ScreenUpdating = True End Sub ' ==== ????????? ==== Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) If KeyCode = 13 Then ' ??? If currentMatches Is Nothing Then Exit Sub If currentMatches.Count = 0 Then Exit Sub ' ?????????(??) currentMatchIndex = (currentMatchIndex + 1) Mod currentMatches.Count application.Goto currentMatches(currentMatchIndex + 1), True End If End Sub ' ==== ?????? ==== Private Sub Worksheet_Deactivate() originalZoom = ActiveWindow.Zoom End Sub ' ==== ?????? ==== Private Sub Worksheet_Activate() If originalZoom > 10 And originalZoom < 400 Then ActiveWindow.Zoom = originalZoom End If End Sub ' ==== ??????? ==== Private Sub TextBox1_LostFocus() Me.TextBox1.Text = "" End Sub ' ==== ??????? ==== Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) application.ScreenUpdating = False ' ????????? Dim h As Hyperlink For Each h In Me.Hyperlinks With h.Range.Font .ThemeColor = xlThemeColorLight1 .Bold = False End With Next h ' ????????? With Target.Range.Font .Color = RGB(255, 0, 0) .Bold = True End With application.ScreenUpdating = True End Sub ' ==== ?????? ==== Private Sub Worksheet_SelectionChange(ByVal Target As Range) On Error GoTo CleanExit If TextBoxUpdateInProgress Then Exit Sub DeleteTextBox"InfoTextBox" If Target.CountLarge > 1 Then Exit Sub If Target.Column <> 3 Or Target.Row < 4 Then Exit Sub Dim searchValue As String: searchValue = Trim(CStr(Target.Value)) If Len(searchValue) = 0 Then Exit Sub ' ?????? TextBoxUpdateInProgress = True application.ScreenUpdating = False application.EnableEvents = False application.Calculation = xlCalculationManual ' ??????????? DeleteTextBox "InfoTextBox" ' ??ZPM008????? Dim crcSheet As Worksheet Set crcSheet = ThisWorkbook.Sheets("ZPM008") Dim lastRowCRC As Long lastRowCRC = crcSheet.Cells(crcSheet.Rows.Count, "D").End(xlUp).Row ' ??????? Dim dataCRC As Variant dataCRC = crcSheet.Range("D1:I" & lastRowCRC).Value ' ?????I?? Dim resultDict As Object Set resultDict = CreateObject("Scripting.Dictionary") Dim i As Long For i = 2 To UBound(dataCRC, 1) ' ??2??? If StrComp(CStr(dataCRC(i, 1)), searchValue, vbTextCompare) = 0 Then Dim cellValue As String cellValue = Trim(CStr(dataCRC(i, 6))) ' I? If Len(cellValue) > 0 Then resultDict(cellValue) = 1 ' ?????? End If End If Next i ' ???????? If resultDict.Count = 0 Then MsgBox "???????: " & searchValue, vbInformation GoTo CleanExit End If ' ??super repair????? Dim Sheet3 As Worksheet Set Sheet3 = ThisWorkbook.Sheets("super repair") Dim lastRowSheet3 As Long lastRowSheet3 = Sheet3.Cells(Sheet3.Rows.Count, "A").End(xlUp).Row ' ??????? Dim dataSheet3 As Variant dataSheet3 = Sheet3.Range("A1:B" & lastRowSheet3).Value ' ????(A??B???) Dim sheet3Map As Object Set sheet3Map = CreateObject("Scripting.Dictionary") For i = 2 To UBound(dataSheet3, 1) ' ??2??? Dim aValue As String: aValue = Trim(CStr(dataSheet3(i, 1))) If Len(aValue) > 0 Then sheet3Map(aValue) = CStr(dataSheet3(i, 2)) End If Next i ' ?????????? Dim finalDict As Object: Set finalDict = CreateObject("Scripting.Dictionary") Dim key As Variant For Each key In resultDict.Keys If sheet3Map.Exists(key) Then finalDict(sheet3Map(key)) = 1 ' ?? End If Next key If finalDict.Count = 0 Then MsgBox "???????????: " & searchValue, vbInformation GoTo CleanExit End If ' ??????? Dim resultString As String For Each key In finalDict.Keys resultString = resultString & key & vbLf Next key resultString = Left(resultString, Len(resultString) - 1) ' ????????? ' ?L????????????? Dim destCell As Range Set destCell = Me.Cells(Target.Row, "L") Dim textBox As Shape Set textBox = Me.Shapes.AddTextbox( _ Orientation:=msoTextOrientationHorizontal, _ Left:=destCell.Left, _ Top:=destCell.Top, _ Width:=destCell.Width * 4, _ Height:=application.Max(40, finalDict.Count * 14)) With textBox .name = "InfoTextBox" .TextFrame.Characters.Text = resultString .Fill.ForeColor.RGB = RGB(255, 255, 230) ' ??? .Line.ForeColor.RGB = RGB(150, 150, 150) ' ??? .TextFrame.HorizontalAlignment = xlHAlignCenter .TextFrame.VerticalAlignment = xlVAlignCenter .TextFrame.Characters.Font.Size = 9 .TextFrame2.AutoSize = msoAutoSizeShapeToFitText .Placement = xlFreeFloating ' ?????? End With CleanExit: ' ???? TextBoxUpdateInProgress = False application.Calculation = xlCalculationAutomatic application.EnableEvents = True application.ScreenUpdating = True If Err.Number <> 0 Then Err.Clear End Sub ' ==== A????? ==== Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range: Set rng = Intersect(Target, Me.Columns("A")) If rng Is Nothing Then Exit Sub application.ScreenUpdating = False application.EnableEvents = False application.Calculation = xlCalculationManual On Error Resume Next Dim wsTemplate As Worksheet Set wsTemplate = ThisWorkbook.Worksheets("Template") If wsTemplate Is Nothing Then MsgBox "???Template???!", vbExclamation GoTo CleanExit End If Dim cell As Range, wsNew As Worksheet For Each cell In rng If Not IsEmpty(cell) Then Dim strName As String strName = CStr(cell.Value) If Not WorksheetExists(strName) Then ' ?????????? wsTemplate.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) Set wsNew = ActiveSheet wsNew.name = strName wsNew.Range("A2").Value = strName ' ????? Me.Hyperlinks.Add _ Anchor:=cell, _ Address:="", _ SubAddress:="'" & strName & "'!A1", _ TextToDisplay:=strName End If End If Next cell CleanExit: application.Calculation = xlCalculationAutomatic application.EnableEvents = True application.ScreenUpdating = True End Sub Private Sub DeleteTextBox(name As String) On Error Resume Next Me.Shapes(name).Delete On Error GoTo 0 End Sub Private Function WorksheetExists(shtName As String) As Boolean On Error Resume Next WorksheetExists = (ThisWorkbook.Worksheets(shtName).name <> "") End Function
最新发布
09-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值