2016-12-30 31 views
-1

您好,我希望輸入元素中的文字在初始點擊時突出顯示。然而我的功能似乎並沒有工作。我研究了這個問題,發現jquery 1.7及以下版本存在一些問題。我已經調整它來解決這個問題,但它仍然不起作用。突出顯示輸入文字不起作用

任何幫助將是偉大的。謝謝!

HTML

<input type="text" value="hello"/> 

JS

$scope.highlightText = function() { 
    $("input[type='text']").on("click", function() { 
     $(this).select(); 
    }); 

https://plnkr.co/edit/b7TYAFQNkhjE6lpRSWTR?p=preview

+1

你的問題是什麼? –

+0

如何獲取輸入元素中的文本以突出顯示當我單擊它時 – RyeGuy

+1

您可以使用ng-click =「highlightText()」 –

回答

2

你需要在控制器端實際調用你的方法,否則該事件未綁定。

https://plnkr.co/edit/a0BlekB8qTGOmWS8asIx?p=preview

...其他代碼...

 $scope.highlightText = function() { 
        $("input[type='text']").on("click", function() { 
         $(this).select(); 
         var test = $(this).parent(); 
         console.log(test); 
        }); 

     $("textarea").on("click", function() { 
         $(this).select(); 
      }); 
     }; 
    $scope.highlightText(); 
    }; 
+0

你的男人! thx – RyeGuy

2

要選擇像圖所示

<input type="text" onclick="this.select()" value="hello"/>
輸入中的文本,你會簡單地調用從 onclick this.select()

+0

這是一個很棒的解決方案。謝謝! – RyeGuy

相關問題