I don't go to TJ much anymore, but I did way back when and probably had a half dozen encounters with the cops. Virtually every time, my behavior contributed to my problems from either carrying around a beer, pissing on a wall, driving recklessly, getting in a fight, etc. I never once went to jail but I often paid them off ($10 - $20).
What I learned help is to be respectful, if they sense that you are an arrogant gringo, they will come down hard on you. Apologize profusely for your offense, don't start arguing with them, just speak calmly with them, maybe joke a little, mention what a great country Mexico is, etc. If they mention jail, ask them if they can pay the judge for you. That's a face saving way of handling it. Don't bring up the bribe, wait for them to bring it up.
All that being said, some of them are the scum of the earth, so be careful.
One other thing, if a fight breaks out, get the hell away from the area. The cops will simply arrest every gringo in sight and they might beat up on you a bit. If you can't get away, give $20 to the bar workers to let you hide in the back.